-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsrv): add structured errors
Structured errors are so incredibly helpful for service owners. With structured errors, you're able to enrich the context of your failures without having to add DEBUG log after DEBUG log, to provide that context. This marks the end of the "training wheels" part of the workshop. Now that we have a somewhat solid foundation to build on, we can start to make informed decisions that help us survive the only constant in software engineering... changing business requirements >_<! We still have a number of concerns that we can't address within the the existing API deisgn... but now we have the tools to inform our server evolution. GEDIUP!
- Loading branch information
Showing
4 changed files
with
48 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package allsrv | ||
|
||
const ( | ||
errTypeExists = "exists" | ||
errTypeNotFound = "not found" | ||
) | ||
|
||
// Err provides a lightly structured error that we can attach behavior. Additionally, | ||
// the use of fields makes it possible for us to enrich our logging infra without | ||
// blowing up the message cardinality. | ||
type Err struct { | ||
Type string | ||
Msg string | ||
Fields []any | ||
} | ||
|
||
// Error returns the error message. | ||
func (e Err) Error() string { | ||
return e.Msg | ||
} | ||
|
||
// ExistsErr creates an exists error. | ||
func ExistsErr(msg string, fields ...any) error { | ||
return Err{ | ||
Type: errTypeExists, | ||
Msg: msg, | ||
Fields: fields, | ||
} | ||
} | ||
|
||
// NotFoundErr creates a not found error. | ||
func NotFoundErr(msg string, fields ...any) error { | ||
return Err{ | ||
Type: errTypeNotFound, | ||
Msg: msg, | ||
Fields: fields, | ||
} | ||
} |
This file contains 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