teleport/errors.go

338 lines
8.2 KiB
Go
Raw Normal View History

2015-10-31 18:56:49 +00:00
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2015-08-25 17:54:16 +00:00
package teleport
import (
"fmt"
2015-08-25 17:54:16 +00:00
"github.com/gravitational/trace"
)
// AlreadyAcquiredError is returned when lock has been acquired
2015-08-25 17:54:16 +00:00
type AlreadyAcquiredError struct {
trace.Traces `json:"traces"`
Message string `json:"message"`
}
// IsAlreadyAcquiredError returns true to indicate that this is AlreadyAcquiredError
func (e *AlreadyAcquiredError) IsAlreadyAcquiredError() bool {
return true
2015-08-25 17:54:16 +00:00
}
// Error returns log friendly description
2015-08-25 17:54:16 +00:00
func (e *AlreadyAcquiredError) Error() string {
if e.Message != "" {
return e.Message
}
return "lock is already aquired"
}
2015-08-25 17:54:16 +00:00
// OrigError returns original error (in this case this is the error itself)
func (e *AlreadyAcquiredError) OrigError() error {
return e
2015-08-25 17:54:16 +00:00
}
// NotFound returns new instance of not found error
func NotFound(message string) *NotFoundError {
return &NotFoundError{
Message: message,
}
}
// NotFoundError indicates that object has not been found
2015-08-25 17:54:16 +00:00
type NotFoundError struct {
trace.Traces `json:"traces"`
Message string `json:"message"`
}
// IsNotFoundError returns true to indicate that is NotFoundError
func (e *NotFoundError) IsNotFoundError() bool {
return true
2015-08-25 17:54:16 +00:00
}
// Error returns log friendly description of an error
2015-08-25 17:54:16 +00:00
func (e *NotFoundError) Error() string {
if e.Message != "" {
return e.Message
}
return "object not found"
}
2015-08-25 17:54:16 +00:00
// OrigError returns original error (in this case this is the error itself)
func (e *NotFoundError) OrigError() error {
return e
2015-08-25 17:54:16 +00:00
}
// IsNotFound returns whether this error indicates that the object is not found
2015-08-25 17:54:16 +00:00
func IsNotFound(e error) bool {
type nf interface {
IsNotFoundError() bool
}
_, ok := e.(nf)
2015-08-25 17:54:16 +00:00
return ok
}
// AlreadyExistsError indicates that there's a duplicate object that already
// exists in the storage/system
2015-08-25 17:54:16 +00:00
type AlreadyExistsError struct {
trace.Traces `json:"traces"`
// Message is user-friendly error message
Message string `json:"message"`
2015-08-25 17:54:16 +00:00
}
// Error returns log-friendly error description
2015-08-25 17:54:16 +00:00
func (n *AlreadyExistsError) Error() string {
if n.Message != "" {
return n.Message
}
return "object already exists"
}
// IsAlreadyExistsError indicates that this error is of AlreadyExists kind
func (AlreadyExistsError) IsAlreadyExistsError() bool {
return true
2015-08-25 17:54:16 +00:00
}
// OrigError returns original error (in this case this is the error itself)
func (e *AlreadyExistsError) OrigError() error {
return e
}
// IsAlreadyExists returns if this is error indicating that object
// already exists
func IsAlreadyExists(e error) bool {
type ae interface {
IsAlreadyExistsError() bool
}
_, ok := e.(ae)
return ok
}
// MissingParameterError indicates that one of the parameters was missing
2015-08-25 17:54:16 +00:00
type MissingParameterError struct {
trace.Traces `json:"traces"`
// Param is the name of the missing parameter
2015-08-25 17:54:16 +00:00
Param string
}
// Error returns log-friendly description of the error
2015-08-25 17:54:16 +00:00
func (m *MissingParameterError) Error() string {
return fmt.Sprintf("missing required parameter '%v'", m.Param)
}
// IsMissingParameterError indicates that this error is of MissingParameter type
func (m *MissingParameterError) IsMissingParameterError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *MissingParameterError) OrigError() error {
return e
}
// IsMissingParameter detects if this error is of MissingParameter kind
func IsMissingParameter(e error) bool {
type ae interface {
IsMissingParameterError() bool
}
_, ok := e.(ae)
return ok
}
2016-02-18 22:32:28 +00:00
// BadParameter returns a new instance of BadParameterError
func BadParameter(name, message string) *BadParameterError {
return &BadParameterError{
Param: name,
Err: message,
}
}
// BadParameterError indicates that something is wrong with passed
// parameter to API method
2015-08-25 17:54:16 +00:00
type BadParameterError struct {
trace.Traces
Param string `json:"param"`
Err string `json:"message"`
2015-08-25 17:54:16 +00:00
}
// Error returrns debug friendly message
2016-02-18 22:32:28 +00:00
func (b *BadParameterError) Error() string {
return fmt.Sprintf("bad parameter '%v', %v", b.Param, b.Err)
2015-08-25 17:54:16 +00:00
}
// OrigError returns original error (in this case this is the error itself)
2016-02-18 22:32:28 +00:00
func (b *BadParameterError) OrigError() error {
return b
}
// IsBadParameterError indicates that error is of bad parameter type
func (b *BadParameterError) IsBadParameterError() bool {
return true
}
// IsBadParameter detects if this error is of BadParameter kind
func IsBadParameter(e error) bool {
type bp interface {
IsBadParameterError() bool
}
_, ok := e.(bp)
return ok
}
// CompareFailedError indicates that compare failed (e.g wrong password or hash)
type CompareFailedError struct {
trace.Traces
// Message is user-friendly error message
Message string `json:"message"`
}
// Error is debug - friendly message
func (e *CompareFailedError) Error() string {
if e.Message != "" {
return e.Message
}
return "compare failed"
}
// OrigError returns original error (in this case this is the error itself)
func (e *CompareFailedError) OrigError() error {
return e
}
// IsCompareFailed detects if this error is of CompareFailed kind
func IsCompareFailed(e error) bool {
type cf interface {
IsCompareFailedError() bool
}
_, ok := e.(cf)
return ok
}
// ReadonlyError indicates that some backend can only read, not write
type ReadonlyError struct {
trace.Traces
Message string `json:"message"`
}
// Error is debug - friendly error message
func (e *ReadonlyError) Error() string {
if e.Message != "" {
return e.Message
}
return "can't modify data in readonly mode"
}
// IsReadonlyError indicates that this error is of Readonly type
func (e *ReadonlyError) IsReadonlyError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *ReadonlyError) OrigError() error {
return e
}
// IsReadonly detects if this error is of ReadonlyError
func IsReadonly(e error) bool {
type ro interface {
IsReadonlyError() bool
}
_, ok := e.(ro)
return ok
}
// AccessDenied returns new access denied error
func AccessDenied(message string) *AccessDeniedError {
return &AccessDeniedError{
Message: message,
}
}
// AccessDeniedError indicates denied access
type AccessDeniedError struct {
trace.Traces
Message string `json:"message"`
}
// Error is debug - friendly error message
func (e *AccessDeniedError) Error() string {
if e.Message != "" {
return e.Message
}
return "access denied"
}
// IsAccessDeniedError indicates that this error is of AccessDenied type
func (e *AccessDeniedError) IsAccessDeniedError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *AccessDeniedError) OrigError() error {
return e
}
// IsAccessDenied detects if this error is of AccessDeniedError
func IsAccessDenied(e error) bool {
type ad interface {
IsAccessDeniedError() bool
}
_, ok := e.(ad)
return ok
}
// ConnectionProblem returns ConnectionProblem
func ConnectionProblem(message string, err error) *ConnectionProblemError {
return &ConnectionProblemError{
Message: message,
Err: err,
}
}
// ConnectionProblemError indicates any network error that has occured
type ConnectionProblemError struct {
trace.Traces
Message string `json:"message"`
Err error `json:"-"`
}
// Error is debug - friendly error message
func (c *ConnectionProblemError) Error() string {
if c.Message != "" {
return c.Message
}
return "connection problem"
}
// IsConnectionProblemError indicates that this error is of ConnectionProblem
func (c *ConnectionProblemError) IsConnectionProblemError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (c *ConnectionProblemError) OrigError() error {
return c
}
// IsConnectionProblem detects if this error is of ConnectionProblemError
func IsConnectionProblem(e error) bool {
type ad interface {
IsConnectionProblemError() bool
}
_, ok := e.(ad)
return ok
}