-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.go
86 lines (73 loc) · 2.07 KB
/
errors.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package fbs
import (
"fmt"
"trpc.group/trpc-go/fbs/internal/ast"
)
// ErrorWithPos wrap error with position information in the
// source file.
type ErrorWithPos struct {
Err error
Pos *ast.Position
}
// Error implements the error interface.
func (e ErrorWithPos) Error() string {
sourcePos := e.GetPos()
return fmt.Sprintf("%s: %v", sourcePos, e.Err)
}
// GetPos returns position information of error.
func (e ErrorWithPos) GetPos() ast.Position {
if e.Pos == nil {
return ast.Position{Filename: "<input>"}
}
return *e.Pos
}
// Unwrap retrieves the original error.
func (e ErrorWithPos) Unwrap() error {
return e.Err
}
// errorHandler stores error to be handled.
type errorHandler struct {
err error
}
// newErrorHandler creates an error handler.
func newErrorHandler() *errorHandler {
return &errorHandler{}
}
// handleErrorWithPos is used mostly by parser and linker to mark error position.
func (e *errorHandler) handleErrorWithPos(pos *ast.Position, format string, args ...interface{}) error {
if e.err != nil {
return e.err
}
err := errorWithPos(pos, format, args...)
e.err = err
return err
}
// handleError is used mostly by lexer. The passed in error err is already set with
// position information.
func (e *errorHandler) handleError(err error) error {
if e.err != nil {
return e.err
}
e.err = err
return err
}
// getError returns the underlying error in error handler.
func (e *errorHandler) getError() error {
return e.err
}
// errorWithPos create an ErrorWithPos out of position information and customized message.
func errorWithPos(pos *ast.Position, format string, args ...interface{}) ErrorWithPos {
return ErrorWithPos{Pos: pos, Err: fmt.Errorf(format, args...)}
}