Skip to content

Commit 4fbe73d

Browse files
committed
Allow errors to be captured
Added Server.SetErrChannel which will be used to send errors fixes #78
1 parent 307370c commit 4fbe73d

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

server.go

+71
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Server struct {
4040
readTimeoutMilliseconds int64
4141
tlsPeerNameFunc TlsPeerNameFunc
4242
datagramPool sync.Pool
43+
errChannel chan error
4344
}
4445

4546
//NewServer returns a new Server
@@ -61,6 +62,11 @@ func (s *Server) SetHandler(handler Handler) {
6162
s.handler = handler
6263
}
6364

65+
//Sets a channel for errors
66+
func (s *Server) SetErrChannel(c chan error) {
67+
s.errChannel = c
68+
}
69+
6470
//Sets the connection timeout for TCP connections, in milliseconds
6571
func (s *Server) SetTimeout(millseconds int64) {
6672
s.readTimeoutMilliseconds = millseconds
@@ -181,6 +187,9 @@ func (s *Server) goAcceptConnection(listener net.Listener) {
181187
}
182188
connection, err := listener.Accept()
183189
if err != nil {
190+
if s.errChannel != nil {
191+
s.errChannel <- &ListenerError{err}
192+
}
184193
continue
185194
}
186195

@@ -207,6 +216,9 @@ func (s *Server) goScanConnection(connection net.Conn) {
207216
if tlsConn, ok := connection.(*tls.Conn); ok {
208217
// Handshake now so we get the TLS peer information
209218
if err := tlsConn.Handshake(); err != nil {
219+
if s.errChannel != nil {
220+
s.errChannel <- &HandshakeError{err, remoteAddr, tlsConn.ConnectionState()}
221+
}
210222
connection.Close()
211223
return
212224
}
@@ -241,6 +253,9 @@ loop:
241253
if scanCloser.Scan() {
242254
s.parser([]byte(scanCloser.Text()), client, tlsPeer)
243255
} else {
256+
if err := scanCloser.Err(); err != nil && s.errChannel != nil {
257+
s.errChannel <- &ScannerError{err, client, tlsPeer}
258+
}
244259
break loop
245260
}
246261
}
@@ -254,6 +269,9 @@ func (s *Server) parser(line []byte, client string, tlsPeer string) {
254269
err := parser.Parse()
255270
if err != nil {
256271
s.lastError = err
272+
if s.errChannel != nil {
273+
s.errChannel <- &ParserError{err}
274+
}
257275
}
258276

259277
logParts := parser.Dump()
@@ -376,3 +394,56 @@ func (s *Server) goParseDatagrams() {
376394
}
377395
}()
378396
}
397+
398+
// Error types
399+
type ListenerError struct {
400+
wrappedError error
401+
}
402+
403+
func (l *ListenerError) Error() string {
404+
return l.wrappedError.Error()
405+
}
406+
407+
func (l *ListenerError) Unwrap() error {
408+
return l.wrappedError
409+
}
410+
411+
type HandshakeError struct {
412+
wrappedError error
413+
RemoteAddr net.Addr
414+
ConnectionState tls.ConnectionState
415+
}
416+
417+
func (l *HandshakeError) Error() string {
418+
return l.wrappedError.Error()
419+
}
420+
421+
func (l *HandshakeError) Unwrap() error {
422+
return l.wrappedError
423+
}
424+
425+
type ScannerError struct {
426+
wrappedError error
427+
Client string
428+
TLSPeer string
429+
}
430+
431+
func (l *ScannerError) Error() string {
432+
return l.wrappedError.Error()
433+
}
434+
435+
func (l *ScannerError) Unwrap() error {
436+
return l.wrappedError
437+
}
438+
439+
type ParserError struct {
440+
wrappedError error
441+
}
442+
443+
func (l *ParserError) Error() string {
444+
return l.wrappedError.Error()
445+
}
446+
447+
func (l *ParserError) Unwrap() error {
448+
return l.wrappedError
449+
}

0 commit comments

Comments
 (0)