aboutsummaryrefslogtreecommitdiff
path: root/lib/http/callback_error_handler.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-04 03:31:44 +0700
committerShulhan <ms@kilabit.info>2021-03-04 03:31:44 +0700
commit6442d1f4055a5a24eed8510de853e226452a4125 (patch)
tree95a3567c35159d629d4208b2303e80c672f3e079 /lib/http/callback_error_handler.go
parentd298e243159243daaf29525d86afa75a25f6d903 (diff)
downloadpakakeh.go-6442d1f4055a5a24eed8510de853e226452a4125.tar.xz
http: refactoring parameters on Callback and CallbackErrorHandler
Previously, the parameters to Callback has three types: the http.ResponseWriter, *http.Request, and []byte for response body. Not only the type names are long, there is no information on the registered Endpoint on the receiver of Callback. This changes wrap the three parameters into single type EndpointRequest with addition field Endpoint, which contains the registered Endpoint. On the CallbackErrorHandler we also have three parameters, but instead of request body we have an error. This changes store the error for CallbackErrorHandler inside EndpointRequest.Error field.
Diffstat (limited to 'lib/http/callback_error_handler.go')
-rw-r--r--lib/http/callback_error_handler.go22
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/http/callback_error_handler.go b/lib/http/callback_error_handler.go
index 199e1a12..6cd44c3e 100644
--- a/lib/http/callback_error_handler.go
+++ b/lib/http/callback_error_handler.go
@@ -18,38 +18,38 @@ import (
// error returned from Endpoint.Call.
// By default, if Endpoint.Call is nil, it will use DefaultErrorHandler.
//
-type CallbackErrorHandler func(http.ResponseWriter, *http.Request, error)
+type CallbackErrorHandler func(epr *EndpointRequest)
//
// DefaultErrorHandler define the default function that will called to handle
// the error returned from Callback function, if the Endpoint.ErrorHandler is
// not defined.
//
-// First, it will check if error instance of errors.E. If its true, it will
+// First, it will check if error instance of *errors.E. If its true, it will
// use the Code value for HTTP status code, otherwise if its zero or invalid,
// it will set to http.StatusInternalServerError.
//
-// Second, it will set the HTTP content-type to "application/json" and write
-// the response body as JSON format,
+// Second, it will set the HTTP header Content-Type to "application/json" and
+// write the response body as JSON format,
//
// {"code":<HTTP_STATUS_CODE>, "message":<err.Error()>}
//
-func DefaultErrorHandler(res http.ResponseWriter, req *http.Request, err error) {
+func DefaultErrorHandler(epr *EndpointRequest) {
errInternal := &liberrors.E{}
- if errors.As(err, &errInternal) {
+ if errors.As(epr.Error, &errInternal) {
if errInternal.Code <= 0 || errInternal.Code >= 512 {
errInternal.Code = http.StatusInternalServerError
}
} else {
log.Printf("DefaultErrorHandler: %d %s %s %s\n",
http.StatusInternalServerError,
- req.Method, req.URL.Path, err)
+ epr.HttpRequest.Method, epr.HttpRequest.URL.Path, epr.Error)
- errInternal = liberrors.Internal(err)
+ errInternal = liberrors.Internal(epr.Error)
}
- res.Header().Set(HeaderContentType, ContentTypeJSON)
- res.WriteHeader(errInternal.Code)
+ epr.HttpWriter.Header().Set(HeaderContentType, ContentTypeJSON)
+ epr.HttpWriter.WriteHeader(errInternal.Code)
rsp, err := json.Marshal(errInternal)
if err != nil {
@@ -57,7 +57,7 @@ func DefaultErrorHandler(res http.ResponseWriter, req *http.Request, err error)
return
}
- _, err = res.Write(rsp)
+ _, err = epr.HttpWriter.Write(rsp)
if err != nil {
log.Println("DefaultErrorHandler: " + err.Error())
}