-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
53 lines (43 loc) · 1.37 KB
/
error.go
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
package cask
import (
"bytes"
"fmt"
)
// Errors represents a group of errors and its context.
type Errors struct {
// context specifies the errors context.
context string
// errors specify a group of errors represented as an array.
errors []error
}
// An unexpectedTokenError represents the error that occurs when an the expected
// tokens doesn't match the actual ones.
type unexpectedTokenError struct {
// expectedTokens specify a group of expected token types.
expectedTokens []TokenType
// actualToken specifies the actual token type.
actualToken TokenType
}
// NewErrors creates a new Errors instance and returns its pointer. Requires
// both Errors.context and Errors.errors to be passed as arguments.
func NewErrors(context string, errors ...error) *Errors {
return &Errors{context, errors}
}
// Error returns all error messages represented as a single string. All errors
// include trailing newlines and prepended context.
func (e *Errors) Error() string {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "%s:\n", e.context)
for _, err := range e.errors {
fmt.Fprintf(&buffer, "%s\n", err.Error())
}
return buffer.String()
}
// Error returns a string representation of the unexpectedTokenError.
func (u *unexpectedTokenError) Error() string {
return fmt.Sprintf(
"expected next token to be of type %v, got %s instead",
u.expectedTokens,
u.actualToken.String(),
)
}