File: error_kind.go

package info (click to toggle)
receptor 1.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: python: 1,643; makefile: 305; sh: 174
file content (32 lines) | stat: -rw-r--r-- 768 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
package utils

import "fmt"

// ErrorWithKind represents an error wrapped with a designation of what kind of error it is.
type ErrorWithKind struct {
	Err  error
	Kind string
}

// Error returns the error text as a string.
func (ek ErrorWithKind) Error() string {
	return fmt.Sprintf("%s error: %v", ek.Kind, ek.Err)
}

// WrapErrorWithKind creates an ErrorWithKind that wraps an underlying error.
func WrapErrorWithKind(err error, kind string) ErrorWithKind {
	return ErrorWithKind{
		Err:  err,
		Kind: kind,
	}
}

// ErrorIsKind returns true if err is an ErrorWithKind of the specified kind, or false otherwise (including if nil).
func ErrorIsKind(err error, kind string) bool {
	ek, ok := err.(ErrorWithKind)
	if !ok {
		return false
	}

	return ek.Kind == kind
}