forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service/rcp): unified error handling (celestiaorg#710)
* error handling is unified for all error types and endpoints * duplicated code is extracted into function * errors are returned as valid JSON, not plain text Co-authored-by: rene <[email protected]>
- Loading branch information
Showing
4 changed files
with
42 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package rpc | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func writeError(w http.ResponseWriter, statusCode int, endpoint string, err error) { | ||
w.WriteHeader(statusCode) | ||
errBody, jerr := json.Marshal(err.Error()) | ||
if jerr != nil { | ||
log.Errorw("serializing error", "endpoint", endpoint, "err", jerr) | ||
return | ||
} | ||
_, werr := w.Write(errBody) | ||
if werr != nil { | ||
log.Errorw("writing response", "endpoint", endpoint, "err", werr) | ||
} | ||
log.Errorw("serving request", "endpoint", endpoint, "err", err) | ||
} |