-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs+core): documented and changed how server errors work
- Loading branch information
1 parent
da5e048
commit a3d8140
Showing
16 changed files
with
661 additions
and
73 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,69 @@ | ||
package akumu | ||
|
||
import "fmt" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type ServerErrorKind string | ||
// ErrServer is the base error that akumu | ||
// will return whenever there's any error | ||
// in the >=500 - <600 range. | ||
type ErrServer struct { | ||
|
||
type ServerError struct { | ||
// Code determines the response's status code. | ||
// This is used to understand what response | ||
// was sent due to that particular error. | ||
Code int | ||
URL string | ||
Text string | ||
Kind ServerErrorKind | ||
Body string | ||
|
||
// Request stores the actual http request that | ||
// failed to execute. This is very useful to | ||
// extract information such as URL or headers | ||
// to understand more about what caused this error. | ||
Request *http.Request | ||
} | ||
|
||
var ( | ||
ServerErrorWriter ServerErrorKind = "writer" | ||
ServerErrorBody ServerErrorKind = "body" | ||
ServerErrorStream ServerErrorKind = "stream" | ||
ServerErrorDefault ServerErrorKind = "default" | ||
// ErrServerWriter determines that the | ||
// server error comes from executing logic | ||
// around [Builder.BodyWriter] or derivates | ||
// that use this method. | ||
ErrServerWriter = errors.New("builder is a body writer") | ||
|
||
// ErrServerBody determines that the | ||
// server error comes from executing logic | ||
// around [Builder.BodyReader] or derivates | ||
// that use this method. | ||
ErrServerBody = errors.New("builder is a body reader") | ||
|
||
// ErrServerStream determines that the | ||
// server error comes from executing logic | ||
// around [Builder.Stream] or derivates | ||
// that use this method. | ||
ErrServerStream = errors.New("builder is a body stream") | ||
|
||
// ErrServerDefault determines that the | ||
// server error comes from executing logic | ||
// around having no body, stream nor writer | ||
// involved in delivering a response. | ||
ErrServerDefault = errors.New("builder is a default no body") | ||
) | ||
|
||
func (err ServerError) Error() string { | ||
// Error implements the error interface | ||
// for a server error. | ||
func (err ErrServer) Error() string { | ||
return fmt.Sprintf( | ||
"%d: %s", | ||
err.Code, | ||
err.Text, | ||
http.StatusText(err.Code), | ||
) | ||
} | ||
|
||
// Is determines if the given target error | ||
// is an [ErrServer]. This is used when dealing | ||
// with errors using [errors.Is] and [errors.As]. | ||
func (err ErrServer) Is(target error) bool { | ||
_, ok := target.(ErrServer) | ||
|
||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package akumu_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/studiolambda/akumu" | ||
) | ||
|
||
func TestServerErrors(t *testing.T) { | ||
req, err := http.NewRequest(http.MethodGet, "/foo/bar", nil) | ||
|
||
if err != nil { | ||
t.Fatalf("unable to build http request: %s", err) | ||
} | ||
|
||
serverErr := akumu.ErrServer{ | ||
Code: http.StatusInternalServerError, | ||
Request: req, | ||
} | ||
|
||
if !errors.Is(serverErr, akumu.ErrServer{}) { | ||
t.Fatalf("server error cannot be resolved to its error type") | ||
} | ||
|
||
serr := akumu.ErrServer{} | ||
ok := errors.As(serverErr, &serr) | ||
|
||
if !ok { | ||
t.Fatalf("server error cannot be resolved to its error type") | ||
} | ||
|
||
if serr.Code != serverErr.Code { | ||
t.Fatalf("resolved server error code is different from original") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
package akumu | ||
|
||
// OnErrorKey is used in the [http.Request]'s context | ||
// to store the hook or callback that will run whenever | ||
// a server error is found. | ||
type OnErrorKey struct{} | ||
|
||
type OnErrorHook func(ServerError) | ||
// OnErrorHook stores the current handler that will take | ||
// care of handling a server error in case it's found. | ||
// | ||
// The `error` is always a [ErrServer] and it's joined with | ||
// either of those: | ||
// - [ErrServerWriter] | ||
// - [ErrServerBody] | ||
// - [ErrServerStream] | ||
// - [ErrServerDefault] | ||
// | ||
// To "extend" the hook, you can make use of composition | ||
// to also call the previous function in the new one, creating | ||
// a chain of handlers. Keep in mind you will need to check | ||
// for `nil` if that was the case. | ||
type OnErrorHook func(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.