-
Notifications
You must be signed in to change notification settings - Fork 495
/
utils.go
66 lines (57 loc) · 1.53 KB
/
utils.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package azureauth
import (
"github.com/juju/errors"
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)
// DataError is a go error that wraps the odataerrors.ODataError response type.
type DataError struct {
*odataerrors.ODataError
}
// Code returns the code from the wrapped DataError.
func (e *DataError) Code() string {
return *e.ODataError.GetErrorEscaped().GetCode()
}
// Message returns the message from the wrapped DataError.
func (e *DataError) Message() string {
msg := e.ODataError.GetErrorEscaped().GetMessage()
if msg != nil {
return *msg
}
return e.ODataError.Message
}
// Error implements the error interface.
func (e *DataError) Error() string {
s := e.Code()
if m := e.Message(); m != "" {
s += ": " + m
}
return s
}
// AsDataError returns a wrapped error that exposes the
// underlying error code and message (if possible).
func AsDataError(err error) (*DataError, bool) {
if err == nil {
return nil, false
}
var dataErr *DataError
if errors.As(err, &dataErr) {
return dataErr, true
}
var apiErr *odataerrors.ODataError
if !errors.As(err, &apiErr) {
return nil, false
}
return &DataError{apiErr}, true
}
// ReportableError returns a wrapped error that exposes the
// underlying error code and message (if possible), or just
// the passed in error.
func ReportableError(err error) error {
var apiErr *odataerrors.ODataError
if !errors.As(err, &apiErr) {
return err
}
return &DataError{apiErr}
}