Skip to content

Instantly share code, notes, and snippets.

@sudaraka94
Last active May 3, 2021 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sudaraka94/0cf778acecb40c33661d66a48d63363a to your computer and use it in GitHub Desktop.
Save sudaraka94/0cf778acecb40c33661d66a48d63363a to your computer and use it in GitHub Desktop.
Custom error struct and helpers
package errors
import "github.com/sirupsen/logrus"
type Operation string
type ErrorType string
const (
NotFoundError ErrorType = "NOT_FOUND"
UnAuthorizedError ErrorType = "UNAUTHORIZED"
Unexpected ErrorType = "UNEXPECTED"
)
type Error struct {
operations []Operation
errorType ErrorType
error error
severity logrus.Level
}
func NewError(operation Operation, errorType ErrorType, err error, severity logrus.Level) *Error{
return &Error{
operations: []Operation{operation},
errorType: errorType,
error: err,
severity: severity,
}
}
func (e *Error) WithOpetation(operation Operation) *Error{
e.operations = append(e.operations, operation)
return e
}
func (e *Error) Operations() []Operation{
return e.operations
}
func (e *Error) ErrorType() ErrorType{
return e.errorType
}
func (e *Error) Error() error{
return e.error
}
func (e *Error) Severity() logrus.Level{
return e.severity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment