Skip to content

Commit 78b5c93

Browse files
committed
doc: adding comments for each method
1 parent 2865d4f commit 78b5c93

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

error.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ type OopsError struct {
4141
stacktrace *oopsStacktrace
4242
}
4343

44+
// Unwrap returns the underlying error.
4445
func (o OopsError) Unwrap() error {
4546
return o.err
4647
}
4748

49+
// Error returns the error message, without context.
4850
func (o OopsError) Error() string {
4951
if o.err != nil {
5052
if o.msg == "" {
@@ -57,6 +59,7 @@ func (o OopsError) Error() string {
5759
return o.msg
5860
}
5961

62+
// Code returns the error cause. Error code is intented to be used by machines.
6063
func (o OopsError) Code() string {
6164
return getDeepestErrorAttribute(
6265
o,
@@ -66,6 +69,7 @@ func (o OopsError) Code() string {
6669
)
6770
}
6871

72+
// Time returns the time when the error occured.
6973
func (o OopsError) Time() time.Time {
7074
return getDeepestErrorAttribute(
7175
o,
@@ -75,6 +79,7 @@ func (o OopsError) Time() time.Time {
7579
)
7680
}
7781

82+
// Duration returns the duration of the error.
7883
func (o OopsError) Duration() time.Duration {
7984
return getDeepestErrorAttribute(
8085
o,
@@ -84,6 +89,7 @@ func (o OopsError) Duration() time.Duration {
8489
)
8590
}
8691

92+
// Domain returns the domain of the error.
8793
func (o OopsError) Domain() string {
8894
return getDeepestErrorAttribute(
8995
o,
@@ -93,6 +99,7 @@ func (o OopsError) Domain() string {
9399
)
94100
}
95101

102+
// Tags returns the tags of the error.
96103
func (o OopsError) Tags() []string {
97104
tags := []string{}
98105

@@ -103,6 +110,7 @@ func (o OopsError) Tags() []string {
103110
return lo.Uniq(tags)
104111
}
105112

113+
// Context returns a k/v context of the error.
106114
func (o OopsError) Context() map[string]any {
107115
return mergeNestedErrorMap(
108116
o,
@@ -112,6 +120,7 @@ func (o OopsError) Context() map[string]any {
112120
)
113121
}
114122

123+
// Trace returns the transaction id, trace id, request id, correlation id, etc.
115124
func (o OopsError) Trace() string {
116125
trace := getDeepestErrorAttribute(
117126
o,
@@ -132,6 +141,7 @@ func (o OopsError) Span() string {
132141
return o.span
133142
}
134143

144+
// Hint returns a hint to the user on how to resolve the error.
135145
func (o OopsError) Hint() string {
136146
return getDeepestErrorAttribute(
137147
o,
@@ -141,6 +151,7 @@ func (o OopsError) Hint() string {
141151
)
142152
}
143153

154+
// Owner identify the owner responsible for resolving the error.
144155
func (o OopsError) Owner() string {
145156
return getDeepestErrorAttribute(
146157
o,
@@ -150,6 +161,7 @@ func (o OopsError) Owner() string {
150161
)
151162
}
152163

164+
// User returns the user id and user data.
153165
func (o OopsError) User() (string, map[string]any) {
154166
userID := getDeepestErrorAttribute(
155167
o,
@@ -167,6 +179,7 @@ func (o OopsError) User() (string, map[string]any) {
167179
return userID, userData
168180
}
169181

182+
// Tenant returns the tenant id and tenant data.
170183
func (o OopsError) Tenant() (string, map[string]any) {
171184
tenantID := getDeepestErrorAttribute(
172185
o,
@@ -184,6 +197,7 @@ func (o OopsError) Tenant() (string, map[string]any) {
184197
return tenantID, tenantData
185198
}
186199

200+
// Stacktrace returns a pretty printed stacktrace of the error.
187201
func (o OopsError) Stacktrace() string {
188202
blocks := []string{}
189203
topFrame := ""
@@ -206,6 +220,7 @@ func (o OopsError) Stacktrace() string {
206220
return "Oops: " + strings.Join(blocks, "\nThrown: ")
207221
}
208222

223+
// Sources returns the source fragments of the error.
209224
func (o OopsError) Sources() string {
210225
blocks := [][]string{}
211226

@@ -238,6 +253,7 @@ func (o OopsError) Sources() string {
238253
)
239254
}
240255

256+
// LogValuer returns a slog.Value for logging.
241257
func (o OopsError) LogValuer() slog.Value {
242258
attrs := []slog.Attr{slog.String("message", o.msg)}
243259

@@ -335,6 +351,7 @@ func (o OopsError) LogValuer() slog.Value {
335351
return slog.GroupValue(attrs...)
336352
}
337353

354+
// ToMap returns a map representation of the error.
338355
func (o OopsError) ToMap() map[string]any {
339356
payload := map[string]any{}
340357

@@ -411,6 +428,7 @@ func (o OopsError) ToMap() map[string]any {
411428
return payload
412429
}
413430

431+
// MarshalJSON implements json.Marshaler.
414432
func (o OopsError) MarshalJSON() ([]byte, error) {
415433
return json.Marshal(o.ToMap())
416434
}

helpers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package oops
22

33
import "github.com/samber/lo"
44

5+
// AsOops checks if an error is an `oops.OopsError` object.
6+
// Alias to errors.As.
57
func AsOops(err error) (OopsError, bool) {
68
return lo.ErrorsAs[OopsError](err)
79
}

0 commit comments

Comments
 (0)