Skip to content

Commit

Permalink
Predefine some common server errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Sep 24, 2021
1 parent 6114b77 commit 8b8e72e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ var errServerStopped = errors.New("the server has been stopped")
// explicit call to its Close method.
var errClientStopped = errors.New("the client has been stopped")

// errEmptyMethod is the error reported for an empty request method name.
var errEmptyMethod = &Error{Code: code.InvalidRequest, Message: "empty method name"}

// errChannelClosed is the error reported to a pending callback when the client
// channel has closed before the call completed.
var errChannelClosed = &Error{Code: code.Cancelled, Message: "client channel terminated"}

// errEmptyBatch is the error reported for an empty request batch.
var errEmptyBatch = &Error{Code: code.InvalidRequest, Message: "empty request batch"}

// ErrConnClosed is returned by a server's push-to-client methods if they are
// called after the client connection is closed.
var ErrConnClosed = errors.New("client connection is closed")
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (s *Server) checkAndAssign(next jmessages) tasks {
} else if !s.versionOK(req.V) {
t.err = ErrInvalidVersion
} else if req.M == "" {
t.err = &Error{Code: code.InvalidRequest, Message: "empty method name"}
t.err = errEmptyMethod
} else if s.setContext(t, id) {
t.m = s.assign(t.ctx, req.M)
if t.m == nil {
Expand Down Expand Up @@ -572,7 +572,7 @@ func (s *Server) stop(err error) {
for id, rsp := range s.call {
rsp.ch <- &jmessage{
ID: json.RawMessage(id),
E: &Error{Message: "client channel terminated", Code: code.Cancelled},
E: errChannelClosed,
}
delete(s.call, id)
}
Expand Down Expand Up @@ -615,7 +615,7 @@ func (s *Server) read(ch receiver) {
} else if derr != nil { // parse failure; report and continue
s.pushError(derr)
} else if len(in) == 0 {
s.pushError(&Error{Code: code.InvalidRequest, Message: "empty request batch"})
s.pushError(errEmptyBatch)
} else {
s.log("Received request batch of size %d (qlen=%d)", len(in), s.inq.Len())
s.inq.PushBack(in)
Expand Down

0 comments on commit 8b8e72e

Please sign in to comment.