File: errors.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,416 kB
  • sloc: sh: 99; makefile: 21
file content (44 lines) | stat: -rw-r--r-- 1,435 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
43
44
package errors

import (
	"encoding/json"
	"fmt"

	"github.com/gophercloud/gophercloud"
)

type ManilaError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Details string `json:"details"`
}

type ErrorDetails map[string]ManilaError

// error types from provider_client.go
func ExtractErrorInto(rawError error, errorDetails *ErrorDetails) (err error) {
	switch e := rawError.(type) {
	case gophercloud.ErrDefault400:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault401:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault403:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault404:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault405:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault408:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault429:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault500:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	case gophercloud.ErrDefault503:
		err = json.Unmarshal(e.ErrUnexpectedResponseCode.Body, errorDetails)
	default:
		err = fmt.Errorf("Unable to extract detailed error message")
	}

	return err
}