-
Notifications
You must be signed in to change notification settings - Fork 26
/
errors.go
205 lines (175 loc) · 5.67 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018-2024 go-m3ua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package m3ua
import (
"errors"
"fmt"
"github.com/wmnsk/go-m3ua/messages"
"github.com/wmnsk/go-m3ua/messages/params"
)
// Error definitions.
var (
ErrSCTPNotAlive = errors.New("SCTP is no longer alive")
ErrInvalidState = errors.New("invalid state")
ErrNotEstablished = errors.New("M3UA Conn not established")
ErrFailedToEstablish = errors.New("failed to establish M3UA Conn")
ErrTimeout = errors.New("timed out")
ErrHeartbeatExpired = errors.New("heartbeat timer expired")
ErrFailedToPeelOff = errors.New("failed to peel off Protocol Data")
ErrFailedToWriteSignal = errors.New("failed to write signal")
)
// ErrInvalidVersion is used if a message with an unsupported version is received.
type ErrInvalidVersion struct {
Ver uint8
}
// NewErrInvalidVersion creates ErrInvalidVersion.
func NewErrInvalidVersion(ver uint8) *ErrInvalidVersion {
return &ErrInvalidVersion{Ver: ver}
}
// Error returns error string with violating version.
func (e *ErrInvalidVersion) Error() string {
return fmt.Sprintf("invalid version: %d", e.Ver)
}
// ErrUnsupportedClass is used if a message with an unexpected or
// unsupported Message Class is received.
type ErrUnsupportedClass struct {
Msg messages.M3UA
}
// NewErrUnsupportedClass creates ErrUnsupportedClass
func NewErrUnsupportedClass(msg messages.M3UA) *ErrUnsupportedClass {
return &ErrUnsupportedClass{Msg: msg}
}
// Error returns error string with message class.
func (e *ErrUnsupportedClass) Error() string {
return fmt.Sprintf("message class unsupported. class: %s", e.Msg.MessageClassName())
}
func (e *ErrUnsupportedClass) first40Octets() []byte {
b, err := e.Msg.MarshalBinary()
if err != nil {
return nil
}
if len(b) < 40 {
return b
}
return b[:40]
}
// ErrUnsupportedMessage is used if a message with an
// unexpected or unsupported Message Type is received.
type ErrUnsupportedMessage struct {
Msg messages.M3UA
}
// NewErrUnsupportedMessage creates ErrUnsupportedMessage
func NewErrUnsupportedMessage(msg messages.M3UA) *ErrUnsupportedMessage {
return &ErrUnsupportedMessage{Msg: msg}
}
// Error returns error string with message class and type.
func (e *ErrUnsupportedMessage) Error() string {
return fmt.Sprintf("message unsupported. class: %s, type: %s", e.Msg.MessageClassName(), e.Msg.MessageTypeName())
}
func (e *ErrUnsupportedMessage) first40Octets() []byte {
b, err := e.Msg.MarshalBinary()
if err != nil {
return nil
}
if len(b) < 40 {
return b
}
return b[:40]
}
// ErrUnexpectedMessage is used if a defined and recognized message is received
// that is not expected in the current state (in some cases, the ASP may optionally
// silently discard the message and not send an Error message).
type ErrUnexpectedMessage struct {
Msg messages.M3UA
}
// NewErrUnexpectedMessage creates ErrUnexpectedMessage
func NewErrUnexpectedMessage(msg messages.M3UA) *ErrUnexpectedMessage {
return &ErrUnexpectedMessage{Msg: msg}
}
// Error returns error string with message class and type.
func (e *ErrUnexpectedMessage) Error() string {
return fmt.Sprintf("unexpected message. class: %s, type: %s", e.Msg.MessageClassName(), e.Msg.MessageTypeName())
}
// ErrInvalidSCTPStreamID is used if a message is received on an unexpected SCTP stream.
type ErrInvalidSCTPStreamID struct {
ID uint16
}
// NewErrInvalidSCTPStreamID creates ErrInvalidSCTPStreamID
func NewErrInvalidSCTPStreamID(id uint16) *ErrInvalidSCTPStreamID {
return &ErrInvalidSCTPStreamID{ID: id}
}
// Error returns error string with violating stream ID.
func (e *ErrInvalidSCTPStreamID) Error() string {
return fmt.Sprintf("invalid SCTP Stream ID: %d", e.ID)
}
// ErrAspIDRequired is used by an SGP in response to an ASP Up message that
// does not contain an ASP Identifier parameter when the SGP requires one..
type ErrAspIDRequired struct{}
// NewErrAspIDRequired creates ErrAspIDRequired
func NewErrAspIDRequired() *ErrAspIDRequired {
return &ErrAspIDRequired{}
}
// Error returns error string.
func (e *ErrAspIDRequired) Error() string {
return "ASP ID required"
}
func (c *Conn) handleErrors(e error) error {
var res messages.M3UA
var errInvalidVersion *ErrInvalidVersion
if errors.As(e, &errInvalidVersion) {
res = messages.NewError(
params.NewErrorCode(params.ErrInvalidVersion),
nil, nil, nil, nil,
)
}
//nolint:errorlint
if err, ok := e.(*ErrUnsupportedClass); ok {
res = messages.NewError(
params.NewErrorCode(params.ErrUnsupportedMessageClass),
nil, nil, nil,
params.NewDiagnosticInformation(err.first40Octets()),
)
}
//nolint:errorlint
if err, ok := e.(*ErrUnsupportedMessage); ok {
res = messages.NewError(
params.NewErrorCode(params.ErrUnsupportedMessageType),
nil, nil, nil,
params.NewDiagnosticInformation(err.first40Octets()),
)
}
var errUnexpectedMessage *ErrUnexpectedMessage
if errors.As(e, &errUnexpectedMessage) {
res = messages.NewError(
params.NewErrorCode(params.ErrUnexpectedMessage),
c.cfg.RoutingContexts,
c.cfg.NetworkAppearance,
params.NewAffectedPointCode(
c.cfg.OriginatingPointCode,
),
nil,
)
}
var errInvalidSCTPStreamID *ErrInvalidSCTPStreamID
if errors.As(e, &errInvalidSCTPStreamID) {
res = messages.NewError(
params.NewErrorCode(params.ErrInvalidStreamIdentifier),
nil, nil, nil, nil,
)
}
var errAspIDRequired *ErrAspIDRequired
if errors.As(e, &errAspIDRequired) {
res = messages.NewError(
params.NewErrorCode(params.ErrAspIdentifierRequired),
nil, nil, nil, nil,
)
}
if res == nil {
return e
}
if _, err := c.WriteSignal(res); err != nil {
return err
}
return nil
}