Skip to content

Commit

Permalink
Define how to respond with an error
Browse files Browse the repository at this point in the history
The main difficulty here is that if we get a command with an invalid id, it's
unclear what to return in the id field. This chooses to return null in that case.
  • Loading branch information
jgraham committed Sep 24, 2020
1 parent db6776c commit 15cd3d6
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions index.bs
Expand Up @@ -126,7 +126,7 @@ Message = (
)

CommandResponse = (
id: uint,
id: uint?,
ResponseData,
)

Expand All @@ -142,7 +142,7 @@ Error = (
"invalid argument"
),
message: text,
stacktrace: text,
stacktrace?: text,
)

CommandResult = {
Expand Down Expand Up @@ -296,9 +296,9 @@ To <dfn>process a command</dfn> given |data|:
the <code>method</code> property of |data|.

1. If |command| is not in the [=set of all command names=], return an
[=Error=] with [=error code=] [=unknown command=].
[=error=] with [=error code=] [=unknown command=].

1. Return an [=Error=] with [=error code=] [=invalid argument=].
1. Return an [=error=] with [=error code=] [=invalid argument=].
</div>

Transport {#transport}
Expand Down Expand Up @@ -490,9 +490,9 @@ To <dfn>handle an incoming message</dfn> given a [=WebSocket connection=]
5. Let |result| be the result of [=process a command|processing a
command=] given |data|.

6. If |result| is an [=Error=], then [=respond with an error=] given
|connection|, |result|, and |parsed|["<code>id</code>"], and finally
return.
6. If |result| is an [=error=], then [=respond with an error=] given
|connection|, |parsed|["<code>id</code>"], and |result|'s [=error code=],
and finally return.

7. Let |response| be a new [=map=] with the following properties:

Expand All @@ -514,12 +514,30 @@ To <dfn>handle an incoming message</dfn> given a [=WebSocket connection=]

<div algorithm>
To <dfn>respond with an error</dfn> given a [=WebSocket connection=]
|connection| and an [=error code=] |code|:
|connection|, |command id| and |error code|:

1. Issue: Form a valid JSON |errorObject| given |code|.
1. If |command id| is not a number, let |command id| be null.

2. [=Send a WebSocket message=] comprised of |errorObject| over
|connection|.
1. Let |error data| be a new map matching the <code>Error</code> production in
the [=local end definition=], with the <code>error</code> field set to
|error code|, the <code>message</code> field set to an
implementation-defined string containing a human-readable definition of the
error that occured and the <code>stacktrace</code> field optionally set to
an implementation-defined string containing a stack trace report of the
active stack frames at the time when the error occurred.

1. Set the value of <code>id</code> on |error data| to |command id|.

1. Assert: |error data| matches the <code>CommandResponse</code> definition in
the [=local end definition=].

1. Let |response| be the result of [=serialize JSON to bytes|serializing JSON
to bytes=] given |error data|.

1. Let |serialized| be the result of [=serialize JSON to
bytes|serializing JSON to bytes=] given |response|.

1. [=Send a WebSocket message=] comprised of |serialized|| over |connection|.

</div>

Expand Down

0 comments on commit 15cd3d6

Please sign in to comment.