forked from graniticio/granitic
/
builder.go
95 lines (71 loc) · 3.01 KB
/
builder.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package serviceerror
import (
"errors"
"fmt"
"github.com/graniticio/granitic/config"
ge "github.com/graniticio/granitic/grncerror"
"github.com/graniticio/granitic/instance"
"github.com/graniticio/granitic/ioc"
"github.com/graniticio/granitic/logging"
)
const (
serviceErrorManagerComponentName = instance.FrameworkPrefix + "ServiceErrorManager"
serviceErrorDecoratorComponentName = instance.FrameworkPrefix + "ServiceErrorSourceDecorator"
errorCodeSourceDecoratorComponentName = instance.FrameworkPrefix + "ErrorCodeSourceDecorator"
)
// Constructs an instance of ServiceErrorManager and registers it as a component.
type ServiceErrorManagerFacilityBuilder struct {
}
// See FacilityBuilder.BuildAndRegister
func (fb *ServiceErrorManagerFacilityBuilder) BuildAndRegister(lm *logging.ComponentLoggerManager, ca *config.ConfigAccessor, cn *ioc.ComponentContainer) error {
manager := new(ge.ServiceErrorManager)
manager.FrameworkLogger = lm.CreateLogger(serviceErrorManagerComponentName)
panicOnMissing, err := ca.BoolVal("ServiceErrorManager.PanicOnMissing")
if err != nil {
return errors.New("Unable to build service error manager " + err.Error())
}
manager.PanicOnMissing = panicOnMissing
cn.WrapAndAddProto(serviceErrorManagerComponentName, manager)
errorDecorator := new(ServiceErrorConsumerDecorator)
errorDecorator.ErrorSource = manager
cn.WrapAndAddProto(serviceErrorDecoratorComponentName, errorDecorator)
codeDecorator := new(ErrorCodeSourceDecorator)
codeDecorator.ErrorSource = manager
cn.WrapAndAddProto(errorCodeSourceDecoratorComponentName, codeDecorator)
definitionsPath, err := ca.StringVal("ServiceErrorManager.ErrorDefinitions")
if err != nil {
return errors.New("Unable to load service error messages from configuration: " + err.Error())
}
if messages, err := fb.loadMessagesFromConfig(definitionsPath, ca); err != nil {
return err
} else {
manager.LoadErrors(messages)
}
return nil
}
// See FacilityBuilder.FacilityName
func (fb *ServiceErrorManagerFacilityBuilder) FacilityName() string {
return "ServiceErrorManager"
}
// See FacilityBuilder.DependsOnFacilities
func (fb *ServiceErrorManagerFacilityBuilder) DependsOnFacilities() []string {
return []string{}
}
func (fb *ServiceErrorManagerFacilityBuilder) loadMessagesFromConfig(dPath string, ca *config.ConfigAccessor) ([]interface{}, error) {
if !ca.PathExists(dPath) {
m := fmt.Sprintf("No error definitions found at config path %s", dPath)
return nil, errors.New(m)
}
i := ca.Value(dPath)
if config.JsonType(i) != config.JsonArray {
m := fmt.Sprintf("Couldn't load error messages from config path %s. Make sure the path exists in your configuration and that %s is an array of string arrays ([][]string)", dPath, dPath)
return nil, errors.New(m)
}
if v, err := ca.Array(dPath); err == nil {
return v, nil
} else {
return nil, err
}
}