1package imapserver
2
3import (
4 "errors"
5 "fmt"
6)
7
8func xcheckf(err error, format string, args ...any) {
9 if err != nil {
10 xserverErrorf("%s: %w", fmt.Sprintf(format, args...), err)
11 }
12}
13
14type userError struct {
15 code string // Optional response code in brackets.
16 err error
17}
18
19func (e userError) Error() string { return e.err.Error() }
20func (e userError) Unwrap() error { return e.err }
21
22func xuserErrorf(format string, args ...any) {
23 panic(userError{err: fmt.Errorf(format, args...)})
24}
25
26func xusercodeErrorf(code, format string, args ...any) {
27 panic(userError{code: code, err: fmt.Errorf(format, args...)})
28}
29
30type serverError struct{ err error }
31
32func (e serverError) Error() string { return e.err.Error() }
33func (e serverError) Unwrap() error { return e.err }
34
35func xserverErrorf(format string, args ...any) {
36 panic(serverError{fmt.Errorf(format, args...)})
37}
38
39type syntaxError struct {
40 line string // Optional line to write before BAD result. For untagged response. CRLF will be added.
41 code string // Optional result code (between []) to write in BAD result.
42 errmsg string // BAD response message.
43 err error // Typically with same info as errmsg, but sometimes more.
44}
45
46func (e syntaxError) Error() string {
47 s := "bad syntax: " + e.errmsg
48 if e.code != "" {
49 s += " [" + e.code + "]"
50 }
51 return s
52}
53func (e syntaxError) Unwrap() error { return e.err }
54
55func xsyntaxErrorf(format string, args ...any) {
56 errmsg := fmt.Sprintf(format, args...)
57 err := errors.New(errmsg)
58 panic(syntaxError{"", "", errmsg, err})
59}
60