Skip to content

Commit

Permalink
Improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed Aug 8, 2023
1 parent 105746a commit 5f2d18f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions iap/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package iap

import "fmt"

type ConnectionError struct {
Err string
type CloseError struct {
Code int
Reason string
}

func (e *ConnectionError) Error() string {
return fmt.Sprintf("connection error: %v", e.Err)
func (e *CloseError) Error() string {
return fmt.Sprintf("connection closed: code %v (%v)", e.Code, e.Reason)
}

type ProtocolError struct {
Expand Down
6 changes: 6 additions & 0 deletions iap/iap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -140,6 +141,11 @@ func Dial(ctx context.Context, opts ...DialOption) (*Conn, error) {
sendWriter: sendWriter,
}
if err := c.readFrame(); err != nil {
var closeError websocket.CloseError
if errors.As(err, &closeError) {
return nil, &CloseError{int(closeError.Code), closeError.Reason}
}

return nil, err
}

Expand Down

0 comments on commit 5f2d18f

Please sign in to comment.