Documentation ¶
Overview ¶
Package semerr contains helpers for creating errors with a meaning. It doesn't modifies the original text of the error.
Index ¶
- func IsTemporaryError(err error) booldeprecated
- func NewBadRequestError(err error) error
- func NewConflictError(err error) error
- func NewForbiddenError(err error) error
- func NewInternalServerError(err error) error
- func NewMultiError(errs ...error) errordeprecated
- func NewNotFoundError(err error) error
- func NewRequestEntityTooLargeError(err error) error
- func NewServiceUnavailableError(err error) error
- func NewStatusGatewayTimeoutError(err error) error
- func NewStatusRequestTimeoutError(err error) error
- func NewTooManyRequestsError(err error) error
- func NewUnauthorizedError(err error) error
- func NewUnimplementedError(err error) error
- func NewUnsupportedMediaTypeError(err error) error
- type BadRequestError
- type ConflictError
- type Error
- type ForbiddenError
- type InternalServerError
- type MultiErr
- type NotFoundError
- type RequestEntityTooLargeError
- type ServiceUnavailableError
- type StatusGatewayTimeoutError
- type StatusRequestTimeoutError
- type TooManyRequestsError
- type UnauthorizedError
- type UnimplementedError
- type UnsupportedMediaTypeError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsTemporaryError
deprecated
IsTemporaryError checks that error has Temporary method and it returns true.
Deprecated: Temporary() interface is unstable.
Example ¶
package main import ( "context" "fmt" "github.com/hedhyw/semerr/pkg/v1/semerr" ) func main() { fmt.Println( "Is context.DeadlineExceeded tmp?", semerr.IsTemporaryError(context.DeadlineExceeded), ) fmt.Println( "Is context.Canceled tmp?", semerr.IsTemporaryError(context.Canceled), ) }
Output: Is context.DeadlineExceeded tmp? true Is context.Canceled tmp? false
func NewBadRequestError ¶
NewBadRequestError wraps err and creates BadRequestError.
It indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
It represents following statuses: - HTTP code: Bad Request (400). - GRPC code: InvalidArgument (3).
If err is nil it returns nil.
func NewConflictError ¶ added in v0.1.1
NewConflictError wraps err and creates ConflictError.
It indicates that the request could not be completed due to a conflict with the current state of the target resource.
It represents following statuses: - HTTP code: Conflict (409). - GRPC code: AlreadyExists (6).
If err is nil it returns nil.
func NewForbiddenError ¶
NewForbiddenError wraps err and creates ForbiddenError.
It indicates that the server understood the request but refuses to fulfill it.
It represents following statuses: - HTTP code: Forbidden (403). - GRPC code: PermissionDenied (7).
If err is nil it returns nil.
func NewInternalServerError ¶
NewInternalServerError wraps err and creates InternalServerError.
It indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
It represents following statuses: - HTTP code: Internal Server Error (500). - GRPC code: Unknown (2).
If err is nil it returns nil.
func NewMultiError
deprecated
NewMultiError creates a error that can hold multiple errors. It skips or nil values. If count of errors is 1, it returns the original value. The main error is the first.
Deprecated: use errors.Join.
Example ¶
This example demonstrates returning of multiple errors.
package main import ( "errors" "fmt" "github.com/hedhyw/semerr/pkg/v1/semerr" ) func main() { const ( errFirst semerr.Error = "first error" errSecond semerr.Error = "second error" ) // All nil errors will be skipped. errMulti := semerr.NewMultiError(errFirst, errSecond, nil) // The first error is the main. fmt.Printf("errMulti is %q\n", errMulti.Error()) fmt.Println("Is first? ", errors.Is(errMulti, errFirst)) fmt.Println("Is second?", errors.Is(errMulti, errSecond)) }
Output: errMulti is "first error; second error" Is first? true Is second? false
func NewNotFoundError ¶
NewNotFoundError wraps err and creates NotFoundError.
It indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
It represents following statuses: - HTTP code: Not Found (404). - GRPC code: NotFound (5).
If err is nil it returns nil.
func NewRequestEntityTooLargeError ¶ added in v0.2.0
NewRequestEntityTooLargeError wraps err and creates RequestEntityTooLargeError.
It indicates that the server is refusing to process a request because the request content is larger than the server
It represents following statuses: - HTTP code: Request Entity Too Large (413). - GRPC code: OutOfRange (11).
If err is nil it returns nil.
func NewServiceUnavailableError ¶
NewServiceUnavailableError wraps err and creates ServiceUnavailableError.
It indicates that the server is not ready to handle the request.
It represents following statuses: - HTTP code: Service Unavailable (503). - GRPC code: Unavailable (14).
If err is nil it returns nil.
func NewStatusGatewayTimeoutError ¶ added in v0.3.0
NewStatusGatewayTimeoutError wraps err and creates StatusGatewayTimeoutError.
It indicates that the server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.
It represents following statuses: - HTTP code: Gateway Timeout (504). - GRPC code: DeadlineExceeded (4).
If err is nil it returns nil.
func NewStatusRequestTimeoutError ¶ added in v0.3.0
NewStatusRequestTimeoutError wraps err and creates StatusRequestTimeoutError.
It indicates that the server did not receive a complete request message within the time that it was prepared to wait.
It represents following statuses: - HTTP code: Request Timeout (408). - GRPC code: Canceled (1).
If err is nil it returns nil.
func NewTooManyRequestsError ¶ added in v0.3.0
NewTooManyRequestsError wraps err and creates TooManyRequestsError.
It indicates the user has sent too many requests in a given amount of time.
It represents following statuses: - HTTP code: Too Many Requests (429). - GRPC code: ResourceExhausted (8).
If err is nil it returns nil.
func NewUnauthorizedError ¶
NewUnauthorizedError wraps err and creates UnauthorizedError.
It indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.
It represents following statuses: - HTTP code: Unauthorized (401). - GRPC code: Unauthenticated (16).
If err is nil it returns nil.
func NewUnimplementedError ¶ added in v0.3.0
NewUnimplementedError wraps err and creates UnimplementedError.
It indicates that the server does not support the functionality required to fulfill the request.
It represents following statuses: - HTTP code: Not Implemented (501). - GRPC code: Unimplemented (12).
If err is nil it returns nil.
func NewUnsupportedMediaTypeError ¶ added in v0.3.0
NewUnsupportedMediaTypeError wraps err and creates UnsupportedMediaTypeError.
It indicates indicates that the origin server is refusing to service the request because the content is in a format not supported by this method on the target resource.
It represents following statuses: - HTTP code: Unsupported Media Type (415). - GRPC code: InvalidArgument (3).
If err is nil it returns nil.
Types ¶
type BadRequestError ¶
type BadRequestError struct {
// contains filtered or unexported fields
}
BadRequestError is not a temporary error.
It indicates that the server cannot or will not process the request due to something that is perceived to be a client error.
It represents following statuses: - HTTP code: Bad Request (400). - GRPC code: InvalidArgument (3).
Example ¶
This example demonstrates with meaningfull errors.
package main import ( "errors" "fmt" "github.com/hedhyw/semerr/pkg/v1/semerr" ) func main() { const errExample semerr.Error = "example error" errBadReq := semerr.NewBadRequestError(errExample) fmt.Println( "Is the text the same?", semerr.NewBadRequestError(errExample).Error() == errExample.Error(), ) fmt.Println( "Is the err original?", errors.Is(errBadReq, errExample), ) fmt.Println( "Is nil handled?", semerr.NewBadRequestError(nil) == nil, ) }
Output: Is the text the same? true Is the err original? true Is nil handled? true
type ConflictError ¶ added in v0.1.1
type ConflictError struct {
// contains filtered or unexported fields
}
ConflictError is not a temporary error.
It indicates that the request could not be completed due to a conflict with the current state of the target resource.
It represents following statuses: - HTTP code: Conflict (409). - GRPC code: AlreadyExists (6).
type Error ¶
type Error string
Error is a constant-like string error.
Example ¶
This example demonstrates using of const error.
package main import ( "fmt" "github.com/hedhyw/semerr/pkg/v1/semerr" ) func main() { const errImmutable semerr.Error = "immutable error" // semerr.Error implements error interface. var _ error = errImmutable fmt.Println(errImmutable.Error()) }
Output: immutable error
type ForbiddenError ¶
type ForbiddenError struct {
// contains filtered or unexported fields
}
ForbiddenError is not a temporary error.
It indicates that the server understood the request but refuses to fulfill it.
It represents following statuses: - HTTP code: Forbidden (403). - GRPC code: PermissionDenied (7).
type InternalServerError ¶
type InternalServerError struct {
// contains filtered or unexported fields
}
InternalServerError is not a temporary error.
It indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
It represents following statuses: - HTTP code: Internal Server Error (500). - GRPC code: Unknown (2).
type NotFoundError ¶
type NotFoundError struct {
// contains filtered or unexported fields
}
NotFoundError is not a temporary error.
It indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
It represents following statuses: - HTTP code: Not Found (404). - GRPC code: NotFound (5).
type RequestEntityTooLargeError ¶ added in v0.2.0
type RequestEntityTooLargeError struct {
// contains filtered or unexported fields
}
RequestEntityTooLargeError is not a temporary error.
It indicates that the server is refusing to process a request because the request content is larger than the server
It represents following statuses: - HTTP code: Request Entity Too Large (413). - GRPC code: OutOfRange (11).
type ServiceUnavailableError ¶
type ServiceUnavailableError struct {
// contains filtered or unexported fields
}
ServiceUnavailableError is a temporary error.
It indicates that the server is not ready to handle the request.
It represents following statuses: - HTTP code: Service Unavailable (503). - GRPC code: Unavailable (14).
type StatusGatewayTimeoutError ¶ added in v0.3.0
type StatusGatewayTimeoutError struct {
// contains filtered or unexported fields
}
StatusGatewayTimeoutError is a temporary error.
It indicates that the server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.
It represents following statuses: - HTTP code: Gateway Timeout (504). - GRPC code: DeadlineExceeded (4).
type StatusRequestTimeoutError ¶ added in v0.3.0
type StatusRequestTimeoutError struct {
// contains filtered or unexported fields
}
StatusRequestTimeoutError is a temporary error.
It indicates that the server did not receive a complete request message within the time that it was prepared to wait.
It represents following statuses: - HTTP code: Request Timeout (408). - GRPC code: Canceled (1).
type TooManyRequestsError ¶ added in v0.3.0
type TooManyRequestsError struct {
// contains filtered or unexported fields
}
TooManyRequestsError is not a temporary error.
It indicates the user has sent too many requests in a given amount of time.
It represents following statuses: - HTTP code: Too Many Requests (429). - GRPC code: ResourceExhausted (8).
type UnauthorizedError ¶
type UnauthorizedError struct {
// contains filtered or unexported fields
}
UnauthorizedError is not a temporary error.
It indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.
It represents following statuses: - HTTP code: Unauthorized (401). - GRPC code: Unauthenticated (16).
type UnimplementedError ¶ added in v0.3.0
type UnimplementedError struct {
// contains filtered or unexported fields
}
UnimplementedError is not a temporary error.
It indicates that the server does not support the functionality required to fulfill the request.
It represents following statuses: - HTTP code: Not Implemented (501). - GRPC code: Unimplemented (12).
type UnsupportedMediaTypeError ¶ added in v0.3.0
type UnsupportedMediaTypeError struct {
// contains filtered or unexported fields
}
UnsupportedMediaTypeError is not a temporary error.
It indicates indicates that the origin server is refusing to service the request because the content is in a format not supported by this method on the target resource.
It represents following statuses: - HTTP code: Unsupported Media Type (415). - GRPC code: InvalidArgument (3).