forked from wmnsk/go-m3ua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
194 lines (166 loc) · 5.54 KB
/
errors.go
File metadata and controls
194 lines (166 loc) · 5.54 KB
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
// 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")
// 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.
ErrAspIDRequired = errors.New("ASP Identifier required")
)
// InvalidVersionError is used if a message with an unsupported version is received.
type InvalidVersionError struct {
Ver uint8
}
// NewInvalidVersionError creates InvalidVersionError.
func NewInvalidVersionError(ver uint8) *InvalidVersionError {
return &InvalidVersionError{Ver: ver}
}
// Error returns error string with violating version.
func (e *InvalidVersionError) Error() string {
return fmt.Sprintf("invalid version: %d", e.Ver)
}
// UnsupportedClassError is used if a message with an unexpected or
// unsupported Message Class is received.
type UnsupportedClassError struct {
Msg messages.M3UA
}
// NewUnsupportedClassError creates UnsupportedClassError
func NewUnsupportedClassError(msg messages.M3UA) *UnsupportedClassError {
return &UnsupportedClassError{Msg: msg}
}
// Error returns error string with message class.
func (e *UnsupportedClassError) Error() string {
return fmt.Sprintf("message class unsupported. class: %s", e.Msg.MessageClassName())
}
func (e *UnsupportedClassError) first40Octets() []byte {
b, err := e.Msg.MarshalBinary()
if err != nil {
return nil
}
if len(b) < 40 {
return b
}
return b[:40]
}
// UnsupportedMessageError is used if a message with an
// unexpected or unsupported Message Type is received.
type UnsupportedMessageError struct {
Msg messages.M3UA
}
// NewUnsupportedMessageError creates UnsupportedMessageError
func NewUnsupportedMessageError(msg messages.M3UA) *UnsupportedMessageError {
return &UnsupportedMessageError{Msg: msg}
}
// Error returns error string with message class and type.
func (e *UnsupportedMessageError) Error() string {
return fmt.Sprintf("message unsupported. class: %s, type: %s", e.Msg.MessageClassName(), e.Msg.MessageTypeName())
}
func (e *UnsupportedMessageError) first40Octets() []byte {
b, err := e.Msg.MarshalBinary()
if err != nil {
return nil
}
if len(b) < 40 {
return b
}
return b[:40]
}
// UnexpectedMessageError 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 UnexpectedMessageError struct {
Msg messages.M3UA
}
// NewUnexpectedMessageError creates UnexpectedMessageError
func NewUnexpectedMessageError(msg messages.M3UA) *UnexpectedMessageError {
return &UnexpectedMessageError{Msg: msg}
}
// Error returns error string with message class and type.
func (e *UnexpectedMessageError) Error() string {
return fmt.Sprintf("unexpected message. class: %s, type: %s", e.Msg.MessageClassName(), e.Msg.MessageTypeName())
}
// InvalidSCTPStreamIDError is used if a message is received on an unexpected SCTP stream.
type InvalidSCTPStreamIDError struct {
ID uint16
}
// NewInvalidSCTPStreamIDError creates InvalidSCTPStreamIDError
func NewInvalidSCTPStreamIDError(id uint16) *InvalidSCTPStreamIDError {
return &InvalidSCTPStreamIDError{ID: id}
}
// Error returns error string with violating stream ID.
func (e *InvalidSCTPStreamIDError) Error() string {
return fmt.Sprintf("invalid SCTP Stream ID: %d", e.ID)
}
func (c *Conn) handleErrors(e error) error {
var res messages.M3UA
var InvalidVersionError *InvalidVersionError
if errors.As(e, &InvalidVersionError) {
res = messages.NewError(
params.NewErrorCode(params.InvalidVersionError),
nil, nil, nil, nil,
)
}
//nolint:errorlint
if err, ok := e.(*UnsupportedClassError); ok {
res = messages.NewError(
params.NewErrorCode(params.UnsupportedMessageErrorClass),
nil, nil, nil,
params.NewDiagnosticInformation(err.first40Octets()),
)
}
//nolint:errorlint
if err, ok := e.(*UnsupportedMessageError); ok {
res = messages.NewError(
params.NewErrorCode(params.UnsupportedMessageErrorType),
nil, nil, nil,
params.NewDiagnosticInformation(err.first40Octets()),
)
}
var UnexpectedMessageError *UnexpectedMessageError
if errors.As(e, &UnexpectedMessageError) {
res = messages.NewError(
params.NewErrorCode(params.UnexpectedMessageError),
c.cfg.RoutingContexts,
c.cfg.NetworkAppearance,
params.NewAffectedPointCode(
c.cfg.OriginatingPointCode,
),
nil,
)
}
var InvalidSCTPStreamIDError *InvalidSCTPStreamIDError
if errors.As(e, &InvalidSCTPStreamIDError) {
res = messages.NewError(
params.NewErrorCode(params.ErrInvalidStreamIdentifier),
nil, nil, nil, nil,
)
}
if errors.Is(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
}