Skip to content

Commit

Permalink
Ensure we're setting proper error codes
Browse files Browse the repository at this point in the history
The majority of errors we are throwing are default InvalidArgumentError
type errors.

Otherwise, we only really have two other errors:

1. Attempting to connect to a space where the name is missing
2. Trying to do something when they have entered a space

I've copied the way that locks used errors.
  • Loading branch information
surminus committed Aug 25, 2023
1 parent a1b16ee commit a658ec9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CURSOR_UPDATE } from './CursorConstants.js';

import type { CursorsOptions, CursorUpdate } from './types.js';
import type { RealtimeMessage } from './utilities/types.js';
import { ERR_OUT_OF_SPACE } from './Errors.js';

type CursorsEventMap = {
update: CursorUpdate;
Expand Down Expand Up @@ -48,7 +49,7 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
const self = await this.space.members.getSelf();

if (!self) {
throw new Error('Must enter a space before setting a cursor update');
throw ERR_OUT_OF_SPACE;
}

const channel = this.getChannel();
Expand Down
20 changes: 16 additions & 4 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,38 @@ export class ErrorInfo extends Error {
}
}

export const ERR_SPACE_NAME_MISSING = new ErrorInfo({
message: 'must have a non-empty name',
code: 101000,
statusCode: 400,
});

export const ERR_OUT_OF_SPACE = new ErrorInfo({
message: 'must enter a space to perform this operation',
code: 101001,
statusCode: 400,
});

export const ERR_LOCK_REQUEST_EXISTS = new ErrorInfo({
message: 'lock request already exists',
code: 40050,
code: 101002,
statusCode: 400,
});

export const ERR_LOCK_IS_LOCKED = new ErrorInfo({
message: 'lock is currently locked',
code: 40051,
code: 101003,
statusCode: 400,
});

export const ERR_LOCK_INVALIDATED = new ErrorInfo({
message: 'lock was invalidated by a concurrent lock request which now holds the lock',
code: 40052,
code: 101004,
statusCode: 400,
});

export const ERR_LOCK_RELEASED = new ErrorInfo({
message: 'lock was released',
code: 40053,
code: 101005,
statusCode: 400,
});
3 changes: 2 additions & 1 deletion src/Locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import EventEmitter, {
import type { SpaceMember } from './types.js';
import type { PresenceMember } from './utilities/types.js';
import type Space from './Space.js';
import { ERR_OUT_OF_SPACE } from './Errors.js';

type LocationsEventMap = {
update: { member: SpaceMember; currentLocation: unknown; previousLocation: unknown };
Expand Down Expand Up @@ -57,7 +58,7 @@ export default class Locations extends EventEmitter<LocationsEventMap> {
const self = await this.space.members.getSelf();

if (!self) {
throw new Error('You must enter a space before setting a location.');
throw ERR_OUT_OF_SPACE;
}

const update: PresenceMember['data'] = {
Expand Down
12 changes: 9 additions & 3 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { Types } from 'ably';
import Space from './Space.js';
import type { Lock, LockRequest, SpaceMember } from './types.js';
import type { PresenceMember } from './utilities/types.js';
import { ERR_LOCK_IS_LOCKED, ERR_LOCK_INVALIDATED, ERR_LOCK_REQUEST_EXISTS, ERR_LOCK_RELEASED } from './Errors.js';
import {
ERR_LOCK_IS_LOCKED,
ERR_LOCK_INVALIDATED,
ERR_LOCK_REQUEST_EXISTS,
ERR_LOCK_RELEASED,
ERR_OUT_OF_SPACE,
} from './Errors.js';
import EventEmitter, {
InvalidArgumentError,
inspect,
Expand Down Expand Up @@ -69,7 +75,7 @@ export default class Locks extends EventEmitter<LockEventMap> {
async acquire(id: string, opts?: LockOptions): Promise<LockRequest> {
const self = await this.space.members.getSelf();
if (!self) {
throw new Error('Must enter a space before acquiring a lock');
throw ERR_OUT_OF_SPACE;
}

// check there isn't an existing PENDING or LOCKED request for the current
Expand Down Expand Up @@ -99,7 +105,7 @@ export default class Locks extends EventEmitter<LockEventMap> {
async release(id: string): Promise<void> {
const self = await this.space.members.getSelf();
if (!self) {
throw new Error('Must enter a space before acquiring a lock');
throw ERR_OUT_OF_SPACE;
}

this.deleteLock(id, self.connectionId);
Expand Down
4 changes: 3 additions & 1 deletion src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Cursors from './Cursors.js';
import Members from './Members.js';
import Locks from './Locks.js';

import { ERR_OUT_OF_SPACE } from './Errors.js';

import { isFunction, isObject } from './utilities/is.js';

import type { SpaceOptions, SpaceMember, ProfileData } from './types.js';
Expand Down Expand Up @@ -168,7 +170,7 @@ class Space extends EventEmitter<SpaceEventsMap> {
const self = await this.members.getSelf();

if (!self) {
throw new Error('You must enter a space before attempting to leave it');
throw ERR_OUT_OF_SPACE;
}

const update = {
Expand Down
3 changes: 2 additions & 1 deletion src/Spaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Types } from 'ably';
import { ERR_SPACE_NAME_MISSING } from './Errors.js';

import Space from './Space.js';

Expand Down Expand Up @@ -30,7 +31,7 @@ class Spaces {

async get(name: string, options?: Subset<SpaceOptions>): Promise<Space> {
if (typeof name !== 'string' || name.length === 0) {
throw new Error('Spaces must have a non-empty name');
throw ERR_SPACE_NAME_MISSING;
}

if (this.spaces[name]) return this.spaces[name];
Expand Down

0 comments on commit a658ec9

Please sign in to comment.