err

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package err implements a library for the app to handle the representation of errors to end users.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppErrCode

type AppErrCode int

AppErrCode defines the type of Application error code Application error codes are made of 4 digits. The first 2 digits denotes the component while the last 2 digits is the error index.

const (
	ErrTimerNotFound AppErrCode = iota + 1300
	ErrTimerAlreadyExists
)

13: business logic

const (
	ErrDatabase AppErrCode = iota + 1200
)

12: database

const (
	ErrUnknown AppErrCode = iota + 1000
)

10: common

const (
	ErrValidation AppErrCode = iota + 1100
)

11: model

type RESTAgent

type RESTAgent interface {
	// HTTP status for the response
	HTTPStatus() int
	// user-facing message for the response
	Msg() string
	// app error code
	Code() AppErrCode
}

RESTAgent handles HTTP status and user-facing message for the response if an error of a certain AppErrCode occurs during RESTful API request.

func GetRESTAgentByError

func GetRESTAgentByError(err error) RESTAgent

GetRESTAgentByError returns a RESTAgent by the given error. It tries to parse the associated AppErrCode and return the RESTAgent accordingly. If no AppErrCode is found, the default RESTAgent for ErrUnknown will be returned.

type SimpleRESTAgent

type SimpleRESTAgent struct {
	// contains filtered or unexported fields
}

SimpleRESTAgent is a simple internal implementation of RESTAgent interface.

func (*SimpleRESTAgent) Code

func (s *SimpleRESTAgent) Code() AppErrCode

Code returns the application error code.

func (*SimpleRESTAgent) HTTPStatus

func (s *SimpleRESTAgent) HTTPStatus() int

HTTPStatus returns the HTTP status code.

func (*SimpleRESTAgent) Msg

func (s *SimpleRESTAgent) Msg() string

Msg returns the user-facing message.

type WithCode

type WithCode struct {
	// contains filtered or unexported fields
}

WithCode implements interface error as a wrapper of an error with AppErrCode.

func New

func New(c AppErrCode, msg string) *WithCode

New creates a new WithCode error with the given AppErrCode and error message.

Example
err := New(ErrUnknown, "Internal server error")
fmt.Println(err)
fmt.Println(err.Code())
Output:

Internal server error
1000

func (*WithCode) Code

func (w *WithCode) Code() AppErrCode

Code returns the AppErrCode associated.

Jump to

Keyboard shortcuts

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