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 30, 2020
1 parent db6776c commit f4d456e
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions index.bs
Expand Up @@ -122,31 +122,24 @@ EmptyParams = { *text }
<pre class="cddl local-cddl">
Message = (
CommandResponse //
ErrorResponse //
Event
)

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

ResponseData = (
Error //
CommandResult
)
value: ResultData,
}

Error = (
ErrorResponse = {
id: uint / null,
error: (
"unknown error" /
"unknown method" /
"invalid argument"
),
message: text,
stacktrace: text,
)

CommandResult = {
value: ResultData,
stacktrace?: text,
}

ResultData = (
Expand Down Expand Up @@ -296,9 +289,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 +483,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 +507,23 @@ 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>ErrorResponse</code>
production in the [=local end definition=], with the <code>id</code> field
set to |command id|, 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. Let |response| be the result of [=serialize JSON to bytes|serializing JSON
to bytes=] given |error data|.

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

</div>

Expand Down

0 comments on commit f4d456e

Please sign in to comment.