From fe15cf054c37ab01590462a8de284fc25e963b99 Mon Sep 17 00:00:00 2001 From: Yotam Nachum Date: Sat, 9 Nov 2019 22:17:53 +0200 Subject: Add a method to generate response from errors --- server.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server.go b/server.go index d4827e7..49bccb5 100644 --- a/server.go +++ b/server.go @@ -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} +} -- cgit v1.2.3