errorutils

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 11 Imported by: 0

README

errorutils

This package helps you to define any error you got when doing any process as an HTTP error with its status code and message. You can also customize the error message but still with the same HTTP status code.

Documentation

Index

Constants

View Source
const (
	SUCCESS                 = "success"
	INTERNAL_SERVER_ERROR   = "internal server error"
	NOT_IMPLEMENTED         = "not implemented"
	BAD_GATEWAY             = "bad gateway"
	SERVICE_UNAVAILABLE     = "service unavailable"
	DATA_NOT_FOUND          = "your requested item is not found"
	METHOD_NOT_ALLOWED      = "method not allowed"
	REQUEST_TIMEOUT         = "request timeout"
	BAD_REQUEST             = "bad request"
	PAYMENT_REQUIRED        = "payment required"
	MINIMUM_LENGTH_REQUIRED = "minimum length not exceeded"
	UNAUTHORIZED            = "unauthorized"
	LOGIN_REQUIRED          = "login required"
	TOKEN_REQUIRED          = "token required"
	INVALID_TOKEN           = "invalid token"
	TOKEN_EXPIRED           = "token expired"
	NO_CONTENT              = "no content"
	MAX_SIZE_EXCEEDED       = "maximum size exceeded"
	ALREADY_EXISTS          = "your item already exist"
	INVALID_PAYLOAD         = "invalid payload"
	NO_PERMISSION           = "you have no permission to access this resource"
)

STATUS

View Source
const (
	FORMAT_DATE_DEFAULT     = "2006-01-02"
	FORMAT_DATETIME_DEFAULT = "2006-01-02 15:04:05"
)

FORMAT TIME

Variables

View Source
var (
	// 5XX
	ErrorInternalServer      = NewHttpError(http.StatusInternalServerError, INTERNAL_SERVER_ERROR)
	ErrorNotImplemented      = NewHttpError(http.StatusNotImplemented, NOT_IMPLEMENTED)
	ErrorBadGateway          = NewHttpError(http.StatusBadGateway, BAD_GATEWAY)
	ErrorServiceNotAvailable = NewHttpError(http.StatusServiceUnavailable, SERVICE_UNAVAILABLE)

	// 4XX
	ErrorBadRequest       = NewHttpError(http.StatusBadRequest, BAD_REQUEST)
	ErrorInvalidPayload   = NewHttpError(http.StatusBadRequest, INVALID_PAYLOAD)
	ErrorUnauthorized     = NewHttpError(http.StatusUnauthorized, UNAUTHORIZED)
	ErrorPaymentRequired  = NewHttpError(http.StatusPaymentRequired, PAYMENT_REQUIRED)
	ErrorForbidden        = NewHttpError(http.StatusForbidden, NO_PERMISSION)
	ErrorNotFound         = NewHttpError(http.StatusNotFound, DATA_NOT_FOUND)
	ErrorMethodNotAllowed = NewHttpError(http.StatusMethodNotAllowed, METHOD_NOT_ALLOWED)
	ErrorRequestTimeout   = NewHttpError(http.StatusRequestTimeout, REQUEST_TIMEOUT)
	ErrorDuplicateData    = NewHttpError(http.StatusConflict, ALREADY_EXISTS)
	ErrorLengthRequired   = NewHttpError(http.StatusLengthRequired, MINIMUM_LENGTH_REQUIRED)
	ErrorMaxSize          = NewHttpError(http.StatusRequestEntityTooLarge, MAX_SIZE_EXCEEDED)
	ErrorLoginRequired    = NewHttpError(http.StatusUnauthorized, LOGIN_REQUIRED)
	ErrorTokenRequired    = NewHttpError(http.StatusUnauthorized, TOKEN_REQUIRED)
	ErrorTokenExpired     = NewHttpError(http.StatusUnauthorized, TOKEN_EXPIRED)
	ErrorInvalidToken     = NewHttpError(http.StatusUnauthorized, INVALID_TOKEN)

	// 2XX
	ErrorNoContent = NewHttpError(http.StatusNoContent, NO_CONTENT)
)

ERROR HTTP

Functions

func GetJsonTagInStruct added in v1.1.0

func GetJsonTagInStruct(fieldName string, structOfField any) string

GetJsonTagInStruct to get the JSON tag of struct field

func GetStatusCode

func GetStatusCode(err error) (int, string)

GetStatusCode to get status code from any errors.

Regardless it's a kind of HttpErrorImpl, or it will return 500 and its error by default if not.
Will return 200 and "success" message if error is nil

func GetValidatorController added in v1.1.0

func GetValidatorController() *validator.Validate

GetValidatorController return validator controller

func ValidateStruct added in v1.1.0

func ValidateStruct(structObj interface{}) error

ValidateStruct to validate struct using Go Validator

Types

type HttpError

type HttpError interface {
	Error() string
	CustomMessage(message ...string) *HttpErrorImpl
}

func DefineSQLError added in v1.1.0

func DefineSQLError(err error) HttpError

func NewHttpError

func NewHttpError(status int, message string) HttpError

NewHttpError creating new custom error that implements error object, with both customized status code (HTTP status) and message.

func ToHttpError

func ToHttpError(err error, statusCode int) HttpError

ToHttpError converting any errors to HttpError

func ValidatePayload added in v1.1.0

func ValidatePayload(request *http.Request, s interface{}) HttpError

ValidatePayload to validate payload when using tag 'validate' (returning HttpError)

type HttpErrorImpl

type HttpErrorImpl struct {
	Status  int
	Message string
}

func (*HttpErrorImpl) CustomMessage

func (e *HttpErrorImpl) CustomMessage(message ...string) *HttpErrorImpl

CustomMessage to define custom message when getting any error that implements HttpError Example:

func Abc() error {
	errorMessage := "this is an example of error message"
	log.Error(errorMessage)
	return ErrorInternalServer.CustomMessage(errorMessage)
}

func Abc2() error {
	if _, err := time.Parse("2006-01-02", "23 Aug 2024"); err != nil {
		log.Error("error when parsing string to time object: ", err)
		return ErrorInternalServer.CustomMessage(err.Error())
	}
	return nil
}

func (*HttpErrorImpl) Error

func (e *HttpErrorImpl) Error() string

Error returning a string of error message

Jump to

Keyboard shortcuts

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