From 9c0263cfa4c42e4bf9d6a32a9ab1374703042c96 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 14 Aug 2023 21:53:51 -0700 Subject: [PATCH] initial commits --- serveerror.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 serveerror.go 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) +}