Skip to content

Commit

Permalink
add the respond for statuscode error
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Mar 26, 2024
1 parent 2105bbb commit 9f27922
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions http/statuscode/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"io"
"net/http"

"github.com/xgfone/go-apiserver/result"
)

// Pre-define some errors with the status code.
Expand Down Expand Up @@ -102,6 +104,15 @@ func (e Error) ToError(err error) error {
return err
}

func (e Error) Respond(responder any) {
switch e {
case (Error{}), (Error{Code: 200}):
result.Ok(nil).Respond(responder)
default:
result.Err(e).Respond(responder)
}
}

// ServeHTTP implements the interface http.Handler.
func (e Error) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch err := e.Err.(type) {
Expand Down

0 comments on commit 9f27922

Please sign in to comment.