/
ReferenceError.go
42 lines (35 loc) · 1.15 KB
/
ReferenceError.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
package refer
// Error when required component dependency cannot be found.
import (
"context"
"fmt"
"github.com/pip-services4/pip-services4-go/pip-services4-commons-go/errors"
"github.com/pip-services4/pip-services4-go/pip-services4-components-go/utils"
)
// NewReferenceError Creates an error instance and assigns its values.
//
// Parameters:
// - ctx context.Context execution context to trace execution through call chain.
// - locator any the locator to find reference to dependent component.
// Returns *errors.ApplicationError
func NewReferenceError(ctx context.Context, locator any) *errors.ApplicationError {
message := fmt.Sprintf("Failed to obtain reference to %v", locator)
e := errors.NewInternalError(getTraceId(ctx), "REF_ERROR", message)
e.WithDetails("locator", locator)
return e
}
// this method used here to avoid circular import
func getTraceId(ctx context.Context) string {
traceId := ctx.Value(utils.TRACE_ID)
if traceId == nil || traceId == "" {
traceId = ctx.Value("trace_id")
if traceId == nil || traceId == "" {
traceId = ctx.Value("traceId")
}
}
if val, ok := traceId.(string); ok {
return val
} else {
return ""
}
}