~yotam/go-gemini

fe15cf054c37ab01590462a8de284fc25e963b99 — Yotam Nachum 2 years ago 9397dcf
Add a method to generate response from errors
1 files changed, 16 insertions(+), 0 deletions(-)

M server.go
M server.go => server.go +16 -0
@@ 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}
}