forked from lokalise/go-lokalise-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
38 lines (32 loc) · 826 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
package lokalise
import (
"errors"
"fmt"
"github.com/go-resty/resty/v2"
)
// Error is an API error.
type Error struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func (r Error) Error() string {
return fmt.Sprintf("API request error %d %s", r.Code, r.Message)
}
type errorResponse struct {
Error Error `json:"error"`
}
// apiError identifies whether the response contains an API error.
func apiError(res *resty.Response) error {
if !res.IsError() {
return nil
}
responseError := res.Error()
if responseError == nil {
return errors.New("lokalise: response marked as error but no data returned")
}
responseErrorModel, ok := responseError.(*errorResponse)
if !ok {
return errors.New("lokalise: response error model unknown")
}
return responseErrorModel.Error
}