-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorDescriptionFactory.go
54 lines (47 loc) · 1.67 KB
/
ErrorDescriptionFactory.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package errors
import (
"fmt"
)
// ErrorDescriptionFactory is a factory to create serializeable ErrorDescription
// from ApplicationException or from arbitrary errors.
// The ErrorDescriptions are used to pass errors through the wire
// between microservices implemented in different languages.
// They allow to restore exceptions on the receiving side close to
// the original type and preserve additional information.
//
// see ErrorDescription
// see ApplicationError
var ErrorDescriptionFactory = &_TErrorDescriptionFactory{}
type _TErrorDescriptionFactory struct{}
// Create creates a serializable ErrorDescription from error object.
//
// Parameters: err error an error object
// Returns: *ErrorDescription a serializeable ErrorDescription object that describes the error.
func (c *_TErrorDescriptionFactory) Create(err any) *ErrorDescription {
return NewErrorDescription(err)
}
// NewErrorDescription creates a serializable ErrorDescription from error object.
//
// Parameters: err any an error object
// Returns: *ErrorDescription a serializeable ErrorDescription object that describes the error.
func NewErrorDescription(err any) *ErrorDescription {
description := &ErrorDescription{
Category: Unknown,
Code: "UNKNOWN",
Status: 500,
Message: "Unknown error",
}
if ex, ok := err.(*ApplicationError); ok {
description.Category = ex.Category
description.Status = ex.Status
description.Code = ex.Code
description.Message = ex.Message
description.Details = ex.Details
description.TraceId = ex.TraceId
description.Cause = ex.Cause
description.StackTrace = ex.StackTrace
} else if err != nil {
description.Message = fmt.Sprintf("%v", err)
}
return description
}