/
errors.go
44 lines (38 loc) · 1.28 KB
/
errors.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
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmhub
import (
"github.com/juju/errors"
"github.com/juju/juju/charmhub/transport"
)
// Handle some of the basic error messages.
func handleBasicAPIErrors(list transport.APIErrors, logger Logger) error {
if len(list) == 0 {
return nil
}
masked := true
defer func() {
// Only log out the error if we're masking the original error, that
// way you can at least find the issue in `debug-log`.
// We do this because the original error message can be huge and
// verbose, like a java stack trace!
if masked {
logger.Errorf("charmhub API error %s:%s", list[0].Code, list[0].Message)
}
}()
switch list[0].Code {
case transport.ErrorCodeNotFound:
return errors.NotFoundf("charm or bundle")
case transport.ErrorCodeNameNotFound:
return errors.NotFoundf("charm or bundle name")
case transport.ErrorCodeResourceNotFound:
return errors.NotFoundf("charm resource")
case transport.ErrorCodeAPIError:
return errors.Errorf("unexpected api error attempting to query charm or bundle from the charmhub store")
case transport.ErrorCodeBadArgument:
return errors.BadRequestf("query argument")
}
// We haven't handled the errors, so just return them.
masked = false
return list
}