Skip to content

Commit

Permalink
feat: Descriptive invalid message errors
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Jun 9, 2022
1 parent 9ba15d0 commit b46379e
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 108 deletions.
7 changes: 7 additions & 0 deletions docs/modules/client.md
Expand Up @@ -29,6 +29,7 @@
- [isMessage](client.md#ismessage)
- [parseMessage](client.md#parsemessage)
- [stringifyMessage](client.md#stringifymessage)
- [validateMessage](client.md#validatemessage)

### Interfaces

Expand Down Expand Up @@ -489,3 +490,9 @@ ___
### stringifyMessage

Re-exports [stringifyMessage](common.md#stringifymessage)

___

### validateMessage

Re-exports [validateMessage](common.md#validatemessage)
25 changes: 24 additions & 1 deletion docs/modules/common.md
Expand Up @@ -41,6 +41,7 @@
- [isMessage](common.md#ismessage)
- [parseMessage](common.md#parsemessage)
- [stringifyMessage](common.md#stringifymessage)
- [validateMessage](common.md#validatemessage)

## Common

Expand Down Expand Up @@ -134,7 +135,9 @@ ___

**isMessage**(`val`): val is ConnectionInitMessage \| ConnectionAckMessage \| PingMessage \| PongMessage \| SubscribeMessage \| NextMessage \| ErrorMessage \| CompleteMessage

Checks if the provided value is a message.
Checks if the provided value is a valid GraphQL over WebSocket message.

**`deprecated`** Use `validateMessage` instead.

#### Parameters

Expand Down Expand Up @@ -189,3 +192,23 @@ Stringifies a valid message ready to be sent through the socket.
#### Returns

`string`

___

### validateMessage

**validateMessage**(`val`): [`Message`](common.md#message)

Validates the message against the GraphQL over WebSocket Protocol.

Invalid messages will throw descriptive errors.

#### Parameters

| Name | Type |
| :------ | :------ |
| `val` | `unknown` |

#### Returns

[`Message`](common.md#message)
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/client.ts.snap
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should report close error even if complete message followed 1`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got \\"malformed\\""`;

exports[`should report close error even if complete message followed 2`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got \\"malformed\\""`;
95 changes: 95 additions & 0 deletions src/__tests__/__snapshots__/common.ts.snap
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should report invalid messages with descriptive errors 1`] = `"Message is missing the 'type' property"`;

exports[`should report invalid messages with descriptive errors 2`] = `"Message is expected to be an object, but got string"`;

exports[`should report invalid messages with descriptive errors 3`] = `"Message is expected to be an object, but got array"`;

exports[`should report invalid messages with descriptive errors 4`] = `"Message is expected to be an object, but got number"`;

exports[`should report invalid messages with descriptive errors 5`] = `"Message is expected to be an object, but got number"`;

exports[`should report invalid messages with descriptive errors 6`] = `"Message is expected to be an object, but got function"`;

exports[`should report invalid messages with descriptive errors 7`] = `"Message is expected to be an object, but got function"`;

exports[`should report invalid messages with descriptive errors 8`] = `"Message is expected to be an object, but got function"`;

exports[`should report invalid messages with descriptive errors 9`] = `"Message is missing the 'type' property"`;

exports[`should report invalid messages with descriptive errors 10`] = `"Message is missing the 'type' property"`;

exports[`should report invalid messages with descriptive errors 11`] = `"Message is missing the 'type' property"`;

exports[`should report invalid messages with descriptive errors 12`] = `"Invalid message 'type' property \\"nuxt\\""`;

exports[`should report invalid messages with descriptive errors 13`] = `"\\"connection_init\\" message expects the 'payload' property to be an object or missing, but got \\"\\""`;

exports[`should report invalid messages with descriptive errors 14`] = `"\\"connection_init\\" message expects the 'payload' property to be an object or missing, but got \\"0\\""`;

exports[`should report invalid messages with descriptive errors 15`] = `"\\"connection_init\\" message expects the 'payload' property to be an object or missing, but got \\"undefined\\""`;

exports[`should report invalid messages with descriptive errors 16`] = `"\\"connection_ack\\" message expects the 'payload' property to be an object or missing, but got \\"\\""`;

exports[`should report invalid messages with descriptive errors 17`] = `"\\"ping\\" message expects the 'payload' property to be an object or missing, but got \\"0\\""`;

exports[`should report invalid messages with descriptive errors 18`] = `"\\"pong\\" message expects the 'payload' property to be an object or missing, but got \\"undefined\\""`;

exports[`should report invalid messages with descriptive errors 19`] = `"\\"subscribe\\" message expects the 'id' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 20`] = `"\\"subscribe\\" message expects the 'id' property to be a string, but got number"`;

exports[`should report invalid messages with descriptive errors 21`] = `"\\"subscribe\\" message requires a non-empty 'id' property"`;

exports[`should report invalid messages with descriptive errors 22`] = `"\\"subscribe\\" message expects the 'payload' property to be an object, but got undefined"`;

exports[`should report invalid messages with descriptive errors 23`] = `"\\"subscribe\\" message expects the 'payload' property to be an object, but got array"`;

exports[`should report invalid messages with descriptive errors 24`] = `"\\"subscribe\\" message expects the 'payload' property to be an object, but got string"`;

exports[`should report invalid messages with descriptive errors 25`] = `"\\"subscribe\\" message payload expects the 'query' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 26`] = `"\\"subscribe\\" message payload expects the 'query' property to be a string, but got number"`;

exports[`should report invalid messages with descriptive errors 27`] = `"\\"subscribe\\" message payload expects the 'query' property to be a string, but got object"`;

exports[`should report invalid messages with descriptive errors 28`] = `"\\"subscribe\\" message payload expects the 'operationName' property to be a string or nullish or missing, but got number"`;

exports[`should report invalid messages with descriptive errors 29`] = `"\\"subscribe\\" message payload expects the 'operationName' property to be a string or nullish or missing, but got object"`;

exports[`should report invalid messages with descriptive errors 30`] = `"\\"subscribe\\" message payload expects the 'variables' property to be a an object or nullish or missing, but got string"`;

exports[`should report invalid messages with descriptive errors 31`] = `"\\"subscribe\\" message payload expects the 'extensions' property to be a an object or nullish or missing, but got string"`;

exports[`should report invalid messages with descriptive errors 32`] = `"\\"subscribe\\" message payload expects the 'extensions' property to be a an object or nullish or missing, but got number"`;

exports[`should report invalid messages with descriptive errors 33`] = `"\\"next\\" message expects the 'id' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 34`] = `"\\"next\\" message expects the 'id' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 35`] = `"\\"next\\" message requires a non-empty 'id' property"`;

exports[`should report invalid messages with descriptive errors 36`] = `"\\"next\\" message expects the 'payload' property to be an object, but got undefined"`;

exports[`should report invalid messages with descriptive errors 37`] = `"\\"next\\" message expects the 'payload' property to be an object, but got string"`;

exports[`should report invalid messages with descriptive errors 38`] = `"\\"error\\" message expects the 'id' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 39`] = `"\\"error\\" message requires a non-empty 'id' property"`;

exports[`should report invalid messages with descriptive errors 40`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got undefined"`;

exports[`should report invalid messages with descriptive errors 41`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got {}"`;

exports[`should report invalid messages with descriptive errors 42`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got \\"\\""`;

exports[`should report invalid messages with descriptive errors 43`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got []"`;

exports[`should report invalid messages with descriptive errors 44`] = `"\\"error\\" message expects the 'payload' property to be an array of GraphQL errors, but got [{\\"iam\\":\\"invalid\\"}]"`;

exports[`should report invalid messages with descriptive errors 45`] = `"\\"complete\\" message expects the 'id' property to be a string, but got undefined"`;

exports[`should report invalid messages with descriptive errors 46`] = `"\\"complete\\" message requires a non-empty 'id' property"`;

exports[`should report invalid messages with descriptive errors 47`] = `"\\"complete\\" message expects the 'id' property to be a string, but got number"`;
4 changes: 2 additions & 2 deletions src/__tests__/client.ts
Expand Up @@ -604,7 +604,7 @@ it('should report close error even if complete message followed', async (done) =
on: {
closed: (err) => {
expect((err as CloseEvent).code).toBe(CloseCode.BadResponse);
expect((err as CloseEvent).reason).toBe('Invalid message');
expect((err as CloseEvent).reason).toMatchSnapshot();

done();
},
Expand All @@ -618,7 +618,7 @@ it('should report close error even if complete message followed', async (done) =
{
next: noop,
error: (err) => {
expect((err as Error).message).toBe('Invalid message');
expect((err as Error).message).toMatchSnapshot();
client.dispose();
},
complete: noop,
Expand Down

0 comments on commit b46379e

Please sign in to comment.