diff --git a/internal/cerr/cerr.go b/internal/cerr/cerr.go new file mode 100644 index 0000000..57f4431 --- /dev/null +++ b/internal/cerr/cerr.go @@ -0,0 +1,28 @@ +package cerr + +// Error allows you to create constant "const" error with type boxing. +type Error string + +// Error implements the error builtin interface. +func (err Error) Error() string { + return string(err) +} + +// Wrap returns a new error which wraps a new error into itself. +func (err Error) Wrap(e error) *WrapError { + return &WrapError{ + base: err, + wrap: e, + } +} + +// WrapError is way to wrap errors recursively. +type WrapError struct { + base error + wrap error +} + +// Error implements the error builtin interface recursively. +func (err *WrapError) Error() string { + return err.base.Error() + ": " + err.wrap.Error() +} diff --git a/internal/config/config.go b/internal/config/config.go index d71b1c9..2634234 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -26,7 +26,7 @@ type Parameter struct { // Method represents a method definition (from api.json) type Method struct { Description string `json:"info"` - Permission [][]string `json:"scope"` + Scope [][]string `json:"scope"` Parameters map[string]*Parameter `json:"in"` Download *bool `json:"download"` } diff --git a/internal/config/service.go b/internal/config/service.go index 34f5407..0b46b18 100644 --- a/internal/config/service.go +++ b/internal/config/service.go @@ -6,8 +6,16 @@ import ( "io" "net/http" "strings" + + "git.xdrm.io/go/aicra/internal/cerr" ) +// ErrReadConfig - a problem ocurred when trying to read the configuration file +const ErrReadConfig = cerr.Error("cannot read config") + +// ErrFormatConfig - a invalid format has been detected +const ErrFormatConfig = cerr.Error("invalid config format") + // Parse builds a service from a json reader and checks for most format errors. func Parse(r io.Reader) (*Service, error) { @@ -15,12 +23,13 @@ func Parse(r io.Reader) (*Service, error) { err := json.NewDecoder(r).Decode(receiver) if err != nil { - return nil, err + return nil, ErrReadConfig.Wrap(err) } + ErrFormatConfig.Wrap(fmt.Errorf("blable")) err = receiver.checkAndFormat("/") if err != nil { - return nil, err + return nil, ErrFormatConfig.Wrap(err) } return receiver, nil diff --git a/server.go b/server.go index 2fd35c9..cd6cfff 100644 --- a/server.go +++ b/server.go @@ -132,7 +132,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { /* (6) Execute handler and return response ---------------------------------------------------------*/ // 1. feed request with configuration scope - request.Scope = methodDef.Permission + request.Scope = methodDef.Scope // 1. execute response := api.NewResponse()