File: error.go

package info (click to toggle)
golang-github-hetznercloud-hcloud-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: sh: 5; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,069 bytes parent folder | download | duplicates (2)
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
package schema

import "encoding/json"

// Error represents the schema of an error response.
type Error struct {
	Code       string          `json:"code"`
	Message    string          `json:"message"`
	DetailsRaw json.RawMessage `json:"details"`
	Details    interface{}
}

// UnmarshalJSON overrides default json unmarshalling.
func (e *Error) UnmarshalJSON(data []byte) (err error) {
	type Alias Error
	alias := (*Alias)(e)
	if err = json.Unmarshal(data, alias); err != nil {
		return
	}
	if e.Code == "invalid_input" {
		details := ErrorDetailsInvalidInput{}
		if err = json.Unmarshal(e.DetailsRaw, &details); err != nil {
			return
		}
		alias.Details = details
	}
	return
}

// ErrorResponse defines the schema of a response containing an error.
type ErrorResponse struct {
	Error Error `json:"error"`
}

// ErrorDetailsInvalidInput defines the schema of the Details field
// of an error with code 'invalid_input'.
type ErrorDetailsInvalidInput struct {
	Fields []struct {
		Name     string   `json:"name"`
		Messages []string `json:"messages"`
	} `json:"fields"`
}