diff options
author | Yotam Nachum <me@yotam.net> | 2019-11-09 22:17:53 +0200 |
---|---|---|
committer | Yotam Nachum <me@yotam.net> | 2019-11-09 22:17:53 +0200 |
commit | fe15cf054c37ab01590462a8de284fc25e963b99 (patch) | |
tree | 106e4b1a446847c67a547eb8f150d16bebc538a8 | |
parent | Add a Gemini Error struct that contain status (diff) | |
download | go-gemini-fe15cf054c37ab01590462a8de284fc25e963b99.tar.gz go-gemini-fe15cf054c37ab01590462a8de284fc25e963b99.zip |
Add a method to generate response from errors
-rw-r--r-- | server.go | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -123,3 +123,19 @@ func writeResponse(conn io.Writer, response Response) error { return nil } + +// ErrorResponse create a response from the given error with the error string as the Meta field. +// If the error is of type gemini.Error, the status will be taken from the status field, +// otherwise it will default to StatusTemporaryFailure. +// If the error is nil, the function will panic. +func ErrorResponse(err error) Response { + if err == nil { + panic("nil error is not a valid parameter") + } + + if ge, ok := err.(Error); ok { + return Response{Status: ge.Status, Meta: ge.Error(), Body: nil} + } + + return Response{Status: StatusTemporaryFailure, Meta: err.Error(), Body: nil} +} |