1package cfclient
2
3//go:generate go run gen_error.go
4
5import (
6	"fmt"
7)
8
9type CloudFoundryError struct {
10	Code        int    `json:"code"`
11	ErrorCode   string `json:"error_code"`
12	Description string `json:"description"`
13}
14
15type CloudFoundryErrorsV3 struct {
16	Errors []CloudFoundryErrorV3 `json:"errors"`
17}
18
19type CloudFoundryErrorV3 struct {
20	Code   int    `json:"code"`
21	Title  string `json:"title"`
22	Detail string `json:"detail"`
23}
24
25// CF APIs v3 can return multiple errors, we take the first one and convert it into a V2 model
26func NewCloudFoundryErrorFromV3Errors(cfErrorsV3 CloudFoundryErrorsV3) CloudFoundryError {
27	if len(cfErrorsV3.Errors) == 0 {
28		return CloudFoundryError{
29			0,
30			"GO-Client-No-Errors",
31			"No Errors in response from V3",
32		}
33	}
34
35	return CloudFoundryError{
36		cfErrorsV3.Errors[0].Code,
37		cfErrorsV3.Errors[0].Title,
38		cfErrorsV3.Errors[0].Detail,
39	}
40}
41
42func (cfErr CloudFoundryError) Error() string {
43	return fmt.Sprintf("cfclient error (%s|%d): %s", cfErr.ErrorCode, cfErr.Code, cfErr.Description)
44}
45
46type CloudFoundryHTTPError struct {
47	StatusCode int
48	Status     string
49	Body       []byte
50}
51
52func (e CloudFoundryHTTPError) Error() string {
53	return fmt.Sprintf("cfclient: HTTP error (%d): %s", e.StatusCode, e.Status)
54}
55