diff --git a/serveerror.go b/serveerror.go new file mode 100644 index 0000000..a543ec9 --- /dev/null +++ b/serveerror.go @@ -0,0 +1,60 @@ +package maymoon + +import ( + "sourcecode.social/reiver/go-errhttp" + + "fmt" + "net/http" +) + +func serveError(responsewriter http.ResponseWriter, err error) { + + if nil == responsewriter { +/////////////// RETURN + return + } + + if nil == err { + http.Error(responsewriter, "Internal Server Error\n\nMissing Error\n", http.StatusInternalServerError) +/////////////// RETURN + return + } + + var e errhttp.Error + { + var casted bool + + e, casted = err.(errhttp.Error) + if !casted { + var msg []byte = []byte("Internal Server Error\n\n") + msg = append(msg, err.Error()...) + msg = append(msg, '\n') + + http.Error(responsewriter, string(msg), http.StatusInternalServerError) +/////////////////////// RETURN + return + } + } + + var statuscode int = e.ErrHTTP() + + var msg []byte + { + var statustext string = http.StatusText(statuscode) + if "" == statustext { + statustext = fmt.Sprintf("HTTP Error %d", statuscode) + } + + msg = append(msg, statustext...) + } + { + var inner error = e.Unwrap() + if nil != inner { + msg = append(msg, "\n\n"...) + msg = append(msg, inner.Error()...) + msg = append(msg, '\n') + } + } + + http.Error(responsewriter, string(msg), statuscode) +}