File: user_error.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (49 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download
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
package errz

import "fmt"

// UserError is an error that happened because the user messed something up:
// - invalid syntax
// - invalid configuration
type UserError struct {
	// Message is a textual description of what's wrong.
	// Must be suitable to show to the user.
	Message string
	// Cause optionally holds an underlying error.
	Cause error
}

func NewUserError(msg string) error {
	return UserError{
		Message: msg,
	}
}

func NewUserErrorf(format string, args ...interface{}) error {
	return NewUserError(fmt.Sprintf(format, args...))
}

func NewUserErrorWithCause(cause error, msg string) error {
	return UserError{
		Message: msg,
		Cause:   cause,
	}
}

func NewUserErrorWithCausef(cause error, format string, args ...interface{}) error {
	return NewUserErrorWithCause(cause, fmt.Sprintf(format, args...))
}

func (e UserError) Error() string {
	if e.Cause == nil {
		return e.Message
	}
	if e.Message == "" {
		return e.Cause.Error()
	}
	return fmt.Sprintf("%s: %v", e.Message, e.Cause)
}

func (e UserError) Unwrap() error {
	return e.Cause
}