File: error.go

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (65 lines) | stat: -rw-r--r-- 1,651 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE.client file for details.

package params

import (
	"fmt"
)

// ErrorCode holds the class of an error in machine-readable format.
// It is also an error in its own right.
type ErrorCode string

func (code ErrorCode) Error() string {
	return string(code)
}

func (code ErrorCode) ErrorCode() ErrorCode {
	return code
}

const (
	ErrNotFound             ErrorCode = "not found"
	ErrForbidden            ErrorCode = "forbidden"
	ErrBadRequest           ErrorCode = "bad request"
	ErrUnauthorized         ErrorCode = "unauthorized"
	ErrAlreadyExists        ErrorCode = "already exists"
	ErrNoAdminCredsProvided ErrorCode = "no admin credentials provided"
	ErrMethodNotAllowed     ErrorCode = "method not allowed"
	ErrServiceUnavailable   ErrorCode = "service unavailable"
	ErrInternalServer       ErrorCode = "internal server error"
)

// Error represents an error - it is returned for any response that fails.
type Error struct {
	Message string    `json:"message,omitempty"`
	Code    ErrorCode `json:"code,omitempty"`
}

// NewError returns a new *Error with the given error code
// and message.
func NewError(code ErrorCode, f string, a ...interface{}) error {
	return &Error{
		Message: fmt.Sprintf(f, a...),
		Code:    code,
	}
}

// Error implements error.Error.
func (e *Error) Error() string {
	return e.Message
}

// ErrorCode holds the class of the error in machine readable format.
func (e *Error) ErrorCode() string {
	return e.Code.Error()
}

// Cause implements errgo.Causer.Cause.
func (e *Error) Cause() error {
	if e.Code != "" {
		return e.Code
	}
	return nil
}