semerr

package
v0.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package semerr contains helpers for creating errors with a meaning. It doesn't modifies the original text of the error.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTemporaryError deprecated

func IsTemporaryError(err error) bool

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

func NewBadRequestError(err error) error

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

func NewConflictError(err error) error

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

func NewForbiddenError(err error) error

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

func NewInternalServerError(err error) error

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

func NewMultiError(errs ...error) error

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

func NewNotFoundError(err error) error

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

func NewRequestEntityTooLargeError(err error) error

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

func NewServiceUnavailableError(err error) error

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

func NewStatusGatewayTimeoutError(err error) error

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

func NewStatusRequestTimeoutError(err error) error

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

func NewTooManyRequestsError(err error) error

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

func NewUnauthorizedError(err error) error

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

func NewUnimplementedError(err error) error

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

func NewUnsupportedMediaTypeError(err error) error

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

func (BadRequestError) Temporary

func (err BadRequestError) Temporary() bool

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).

func (ConflictError) Temporary added in v0.1.1

func (err ConflictError) Temporary() bool

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

func (Error) Error

func (err Error) Error() string

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).

func (ForbiddenError) Temporary

func (err ForbiddenError) Temporary() bool

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).

func (InternalServerError) Temporary

func (err InternalServerError) Temporary() bool

type MultiErr

type MultiErr []error

MultiErr is a slice of errors.

func (MultiErr) Error

func (m MultiErr) Error() string

func (MultiErr) Unwrap

func (err MultiErr) Unwrap() error

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).

func (NotFoundError) Temporary

func (err NotFoundError) Temporary() bool

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).

func (RequestEntityTooLargeError) Temporary added in v0.2.0

func (err RequestEntityTooLargeError) Temporary() bool

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).

func (ServiceUnavailableError) Temporary

func (err ServiceUnavailableError) Temporary() bool

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).

func (StatusGatewayTimeoutError) Temporary added in v0.3.0

func (err StatusGatewayTimeoutError) Temporary() bool

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).

func (StatusRequestTimeoutError) Temporary added in v0.3.0

func (err StatusRequestTimeoutError) Temporary() bool

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).

func (TooManyRequestsError) Temporary added in v0.3.0

func (err TooManyRequestsError) Temporary() bool

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).

func (UnauthorizedError) Temporary

func (err UnauthorizedError) Temporary() bool

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).

func (UnimplementedError) Temporary added in v0.3.0

func (err UnimplementedError) Temporary() bool

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).

func (UnsupportedMediaTypeError) Temporary added in v0.3.0

func (err UnsupportedMediaTypeError) Temporary() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL