forked from samber/oops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
38 lines (36 loc) · 1.2 KB
/
helpers.go
File metadata and controls
38 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package oops
import "errors"
// AsOops checks if an error is an oops.OopsError instance and returns it if so.
// This function is an alias to errors.As and provides a convenient way to
// type-assert errors to oops.OopsError without importing the errors package.
//
// This function is useful when you need to access the rich metadata and
// context information stored in oops.OopsError instances, such as error
// codes, stacktraces, user information, or custom context data.
//
// Example usage:
//
// err := someFunction()
// if oopsErr, ok := oops.AsOops(err); ok {
// // Access oops-specific information
// fmt.Printf("Error code: %s\n", oopsErr.Code())
// fmt.Printf("Domain: %s\n", oopsErr.Domain())
// fmt.Printf("Stacktrace: %s\n", oopsErr.Stacktrace())
//
// // Check for specific tags
// if oopsErr.HasTag("critical") {
// // Handle critical errors differently
// sendAlert(oopsErr)
// }
// }
//
// // Chain with other error handling
// if oopsErr, ok := oops.AsOops(err); ok && oopsErr.Code() == "database_error" {
// // Handle database errors specifically
// retryOperation()
// }
func AsOops(err error) (OopsError, bool) {
var e OopsError
ok := errors.As(err, &e)
return e, ok
}