Documentation
¶
Index ¶
- func GenericErr(msg string, err error) error
- func InternalServerErr(err error) error
- func NotFoundErr(err error) error
- func UnauthorizedErr(err error) error
- func ValidationErr(errors ErrorMap) error
- func WrapIfContextDone(ctx context.Context, err error) error
- func WrapIfContextError(err error) error
- func WrapIfLikelyH2CNotConfiguredError(request *http.Request, err error) error
- func WrapIfLikelyWithGRPCNotUsedError(err error) error
- func WrapIfMaxBytesError(err error, tmpl string, args ...any) error
- func WrapIfRSTError(err error) error
- func WrapIfUncoded(err error) error
- type ErrBuilder
- func (err *ErrBuilder) ErrCode() ErrCode
- func (builder *ErrBuilder) Error() string
- func (builder *ErrBuilder) MarshalJSON() ([]byte, error)
- func (err *ErrBuilder) Unwrap() error
- func (builder *ErrBuilder) WithCause(Cause error) *ErrBuilder
- func (builder *ErrBuilder) WithCode(code ErrCode) *ErrBuilder
- func (builder *ErrBuilder) WithDetails(details ErrDetails) *ErrBuilder
- func (builder *ErrBuilder) WithLabel(label string) *ErrBuilder
- func (builder *ErrBuilder) WithMsg(msg string) *ErrBuilder
- type ErrCode
- type ErrDetails
- type ErrorMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenericErr ¶ added in v1.2.0
func InternalServerErr ¶
func NotFoundErr ¶
func UnauthorizedErr ¶
func ValidationErr ¶
Example usage:
app.Post("/register", func(c *http.Ctx) error {
// Define a struct for the request body
type User struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
// simple validation for example purposes
var user User
// Parse the JSON body
if err := c.BodyParser(&user); err != nil {
return InvalidDataErr(err)
}
// Validate the data
var errs errsx.ErrorMap
if len(user.Username) < 4 {
errs.Set("username", "Username must be at least 4 characters")
}
if len(user.Password) < 8 {
errs.Set("password", "Password must be at least 8 characters")
}
if len(user.Email) == 0 {
errs.Set("email", "Email is required")
}
// Check if there were any errors
if errs != nil {
// Return the errors as a JSON response
return c.Status(http.StatusUnprocessableEntity).JSON(ValidationErr(errors))
}
// Continue with user registration process...
return c.SendStatus(http.StatusOK)
})
func WrapIfContextDone ¶
wrapIfContextDone wraps errors with CodeCanceled or CodeDeadlineExceeded if the context is done. It leaves already-wrapped errors unchanged.
func WrapIfContextError ¶
wrapIfContextError applies CodeCanceled or CodeDeadlineExceeded to Go's context.Canceled and context.DeadlineExceeded errors, but only if they haven't already been wrapped.
func WrapIfLikelyH2CNotConfiguredError ¶
wrapIfLikelyH2CNotConfiguredError adds a wrapping error that has a message telling the caller that they likely need to use h2c but are using a raw http.Client{}.
This happens when running a gRPC-only server. This is fragile and may break over time, and this should be considered a best-effort.
func WrapIfLikelyWithGRPCNotUsedError ¶
wrapIfLikelyWithGRPCNotUsedError adds a wrapping error that has a message telling the caller that they likely forgot to use WithGRPC().
This happens when running a gRPC-only server. This is fragile and may break over time, and this should be considered a best-effort.
func WrapIfMaxBytesError ¶
wrapIfMaxBytesError wraps errors returned reading from a http.MaxBytesHandler whose limit has been exceeded.
func WrapIfRSTError ¶
HTTP/2 has its own set of error codes, which it sends in RST_STREAM frames. When the server sends one of these errors, we should map it back into our RPC error codes following https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#http2-transport-mapping.
This would be vastly simpler if we were using x/net/http2 directly, since the StreamError type is exported. When x/net/http2 gets vendored into net/http, though, all these types become unexported...so we're left with string munging.
func WrapIfUncoded ¶
wrapIfUncoded ensures that all errors are wrapped. It leaves already-wrapped errors unchanged, uses wrapIfContextError to apply codes to context.Canceled and context.DeadlineExceeded, and falls back to wrapping other errors with CodeUnknown.
Types ¶
type ErrBuilder ¶
type ErrBuilder struct {
Code ErrCode `json:"code"`
Msg string `json:"message"`
Cause error `json:"Cause"`
Label string `json:"label"`
Details ErrDetails `json:"details"`
}
func (*ErrBuilder) ErrCode ¶
func (err *ErrBuilder) ErrCode() ErrCode
Code returns the error's status code.
func (*ErrBuilder) Error ¶
func (builder *ErrBuilder) Error() string
Error is a method to return an error, this is an implementation of the error interface.
func (*ErrBuilder) MarshalJSON ¶
func (builder *ErrBuilder) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*ErrBuilder) Unwrap ¶
func (err *ErrBuilder) Unwrap() error
Unwrap allows errors.Is and errors.As access to the underlying error.
func (*ErrBuilder) WithCause ¶
func (builder *ErrBuilder) WithCause(Cause error) *ErrBuilder
WithCause is a method to set the error Cause.
func (*ErrBuilder) WithCode ¶
func (builder *ErrBuilder) WithCode(code ErrCode) *ErrBuilder
WithCode is a method to set the error code.
func (*ErrBuilder) WithDetails ¶
func (builder *ErrBuilder) WithDetails(details ErrDetails) *ErrBuilder
WithDetails is a method to set the error details.
func (*ErrBuilder) WithLabel ¶
func (builder *ErrBuilder) WithLabel(label string) *ErrBuilder
WithLabel is a method to set the error label.
func (*ErrBuilder) WithMsg ¶
func (builder *ErrBuilder) WithMsg(msg string) *ErrBuilder
WithMsg is a method to set the error message.
type ErrCode ¶
type ErrCode uint32
const ( // CodeCanceled indicates that the operation was canceled, typically by the // caller. CodeCanceled ErrCode = 1 // CodeUnknown indicates that the operation failed for an unknown reason. CodeUnknown ErrCode = 2 // CodeInvalidArgument indicates that client supplied an invalid argument. CodeInvalidArgument ErrCode = 3 // CodeDeadlineExceeded indicates that deadline expired before the operation // could complete. CodeDeadlineExceeded ErrCode = 4 // CodeNotFound indicates that some requested entity (for example, a file or // directory) was not found. CodeNotFound ErrCode = 5 // CodeAlreadyExists indicates that client attempted to create an entity (for // example, a file or directory) that already exists. CodeAlreadyExists ErrCode = 6 // CodePermissionDenied indicates that the caller doesn't have permission to // execute the specified operation. CodePermissionDenied ErrCode = 7 // CodeResourceExhausted indicates that some resource has been exhausted. For // example, a per-user quota may be exhausted or the entire file system may // be full. CodeResourceExhausted ErrCode = 8 // CodeFailedPrecondition indicates that the system is not in a state // required for the operation's execution. CodeFailedPrecondition ErrCode = 9 // CodeAborted indicates that operation was aborted by the system, usually // because of a concurrency issue such as a sequencer check failure or // transaction abort. CodeAborted ErrCode = 10 // CodeOutOfRange indicates that the operation was attempted past the valid // range (for example, seeking past end-of-file). CodeOutOfRange ErrCode = 11 // CodeUnimplemented indicates that the operation isn't implemented, // supported, or enabled in this service. CodeUnimplemented ErrCode = 12 // CodeInternal indicates that some invariants expected by the underlying // system have been broken. This code is reserved for serious errors. CodeInternal ErrCode = 13 // is usually temporary, so clients can back off and retry idempotent // operations. CodeUnavailable ErrCode = 14 // CodeDataLoss indicates that the operation has resulted in unrecoverable // data loss or corruption. CodeDataLoss ErrCode = 15 // CodeUnauthenticated indicates that the request does not have valid // authentication credentials for the operation. CodeUnauthenticated ErrCode = 16 )
func CodeOf ¶
CodeOf returns the error's status code if it is or wraps an *ErrBuilder and CodeUnknown otherwise.
func (ErrCode) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*ErrCode) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type ErrDetails ¶
type ErrDetails struct {
Errors ErrorMap `json:"errors"`
}
func NewErrDetails ¶
func NewErrDetails(errors ErrorMap) ErrDetails
NewErrDetails is a constructor for ErrDetails
func (*ErrDetails) UnWrap ¶
func (err *ErrDetails) UnWrap() (ErrorMap, error)
UnWrap is a method to return the error details as a map of errors.
type ErrorMap ¶
ErrorMap represents a collection of errors keyed by name.
func (ErrorMap) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.