-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
166 lines (126 loc) · 3.04 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package tgo
import (
"runtime"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type errorType int
const (
errorTypeEmptyResponse errorType = iota
errorTypeStringResponse
)
type tgBotError interface {
errorType() errorType
}
var (
_ tgBotError = (*MessageError)(nil)
_ tgBotError = (*ExceptionError)(nil)
)
type MessageError struct {
message string
replyToMessageID int
editMessage *tgbotapi.Message
parseMode string
replyMarkup *tgbotapi.InlineKeyboardMarkup
deleteLaterForUserID int64
deleteLaterChatID int64
}
func NewMessageError(message string) MessageError {
return MessageError{
message: message,
}
}
func (e MessageError) errorType() errorType {
return errorTypeStringResponse
}
func (e MessageError) Error() string {
return e.message
}
func (e MessageError) WithReply(message *tgbotapi.Message) MessageError {
if message == nil {
return e
}
e.replyToMessageID = message.MessageID
return e
}
func (e MessageError) WithDeleteLater(userID int64, chatID int64) MessageError {
e.deleteLaterForUserID = userID
e.deleteLaterChatID = chatID
return e
}
func (e MessageError) WithEdit(message *tgbotapi.Message) MessageError {
if message == nil {
return e
}
e.editMessage = message
return e
}
func (e MessageError) WithParseModeHTML() MessageError {
e.parseMode = tgbotapi.ModeHTML
return e
}
func (e MessageError) WithReplyMarkup(replyMarkup tgbotapi.InlineKeyboardMarkup) MessageError {
e.replyMarkup = &replyMarkup
return e
}
type ExceptionError struct {
err error
message string
replyToMessageID int
editMessage *tgbotapi.Message
callFrameSkip int
callFrame *runtime.Frame
replyMarkup *tgbotapi.InlineKeyboardMarkup
deleteLaterForUserID int64
deleteLaterChatID int64
}
func NewExceptionError(err error) ExceptionError {
e := ExceptionError{
err: err,
callFrameSkip: 1,
}
pc, file, line, _ := runtime.Caller(e.callFrameSkip)
funcDetails := runtime.FuncForPC(pc)
var funcName string
if funcDetails != nil {
funcName = funcDetails.Name()
}
e.callFrame = &runtime.Frame{
File: file,
Line: line,
Function: funcName,
}
return e
}
func (e ExceptionError) errorType() errorType {
return errorTypeEmptyResponse
}
func (e ExceptionError) Error() string {
return e.err.Error()
}
func (e ExceptionError) WithMessage(message string) ExceptionError {
e.message = message
return e
}
func (e ExceptionError) WithReply(message *tgbotapi.Message) ExceptionError {
if message == nil {
return e
}
e.replyToMessageID = message.MessageID
return e
}
func (e ExceptionError) WithEdit(message *tgbotapi.Message) ExceptionError {
if message == nil {
return e
}
e.editMessage = message
return e
}
func (e ExceptionError) WithReplyMarkup(replyMarkup tgbotapi.InlineKeyboardMarkup) ExceptionError {
e.replyMarkup = &replyMarkup
return e
}
func (e ExceptionError) WithDeleteLater(userID int64, chatID int64) ExceptionError {
e.deleteLaterForUserID = userID
e.deleteLaterChatID = chatID
return e
}