-
Notifications
You must be signed in to change notification settings - Fork 72
/
error.go
43 lines (32 loc) · 932 Bytes
/
error.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
package registrycmdutil
import (
"errors"
"fmt"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
)
type Error struct {
Err error
}
func (e *Error) Error() string {
return fmt.Sprint(e.Err)
}
func (e *Error) Unwrap() error {
return e.Err
}
// GetInstanceAPIError gets a strongly typed error from an error
func GetInstanceAPIError(err error) (e registryinstanceclient.Error, ok bool) {
var apiError registryinstanceclient.GenericOpenAPIError
if ok = errors.As(err, &apiError); ok {
errModel := apiError.Model()
e, ok = errModel.(registryinstanceclient.Error)
}
return e, ok
}
// TransformInstanceError code contains message that can be returned to the user
func TransformInstanceError(err error) error {
mappedErr, ok := GetInstanceAPIError(err)
if !ok {
return err
}
return errors.New(mappedErr.GetName() + ": " + mappedErr.GetMessage())
}