Last active
May 3, 2021 05:28
-
-
Save sudaraka94/0cf778acecb40c33661d66a48d63363a to your computer and use it in GitHub Desktop.
Custom error struct and helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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