Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Unwrap ¶
Unwrap recursion unwraps an error and returns *errcode.Error
Unwrap returns inputted error if not found any *errcode.Error
Example ¶
// Define error ec := NewError(4, http.StatusBadRequest, "Invalid %v") // Wrap error in many times (on many functions) for easy tracking err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username"))) // Unwrap to get code, http status code and message to respond to the client u := Unwrap(err) fmt.Println(err) fmt.Println(u)
Output: service: repo: 4 - Invalid username 4 - Invalid username
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func HardUnwrap ¶
HardUnwrap is errcode.Unwrap but returns *errcode.Error instead of error
Example ¶
// Define error ec := NewError(5, http.StatusBadRequest, "Invalid %v") // Wrap error in many times (on many functions) for easy tracking err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username"))) // Unwrap to get code, http status code and message to respond to the client u, ok := HardUnwrap(err) fmt.Println(err) fmt.Println(u) fmt.Println(ok) fmt.Println(u.Code()) fmt.Println(u.HTTPStatusCode()) fmt.Println(u.Message())
Output: service: repo: 5 - Invalid username 5 - Invalid username true 5 400 Invalid username
func NewError ¶
NewError create new *errcode.Error with unique code
NewError panic if found duplicate code
Example ¶
err := NewError(1, http.StatusInternalServerError, "Internal Server Error") fmt.Println(err)
Output: 1 - Internal Server Error
func (*Error) HTTPStatusCode ¶
Example ¶
err := NewError(2, http.StatusNotFound, "Not Found") fmt.Println(err.HTTPStatusCode())
Output: 404
Click to show internal directories.
Click to hide internal directories.