Skip to content

Commit

Permalink
Merge pull request #1966 from ably/PUB-1011/update-error-codes
Browse files Browse the repository at this point in the history
[PUB-1011] Use more specific error codes for LiveObjects errors
  • Loading branch information
VeskeR authored Feb 13, 2025
2 parents 19ab536 + bf842fa commit 6fc1702
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/plugins/liveobjects/batchcontextlivecounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class BatchContextLiveCounter {
// do an explicit type safety check here before negating the amount value,
// so we don't unintentionally change the type sent by a user
if (typeof amount !== 'number') {
throw new this._client.ErrorInfo('Counter value decrement should be a number', 40013, 400);
throw new this._client.ErrorInfo('Counter value decrement should be a number', 40003, 400);
}

this.increment(-amount);
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/liveobjects/livecounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
const client = liveObjects.getClient();

if (typeof amount !== 'number' || !isFinite(amount)) {
throw new client.ErrorInfo('Counter value increment should be a valid number', 40013, 400);
throw new client.ErrorInfo('Counter value increment should be a valid number', 40003, 400);
}

const stateMessage = StateMessage.fromValues(
Expand All @@ -77,7 +77,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
const client = liveObjects.getClient();

if (count !== undefined && (typeof count !== 'number' || !Number.isFinite(count))) {
throw new client.ErrorInfo('Counter value should be a valid number', 40013, 400);
throw new client.ErrorInfo('Counter value should be a valid number', 40003, 400);
}

const initialValueObj = LiveCounter.createInitialValueObject(count);
Expand Down Expand Up @@ -150,7 +150,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
// do an explicit type safety check here before negating the amount value,
// so we don't unintentionally change the type sent by a user
if (typeof amount !== 'number' || !isFinite(amount)) {
throw new this._client.ErrorInfo('Counter value decrement should be a valid number', 40013, 400);
throw new this._client.ErrorInfo('Counter value decrement should be a valid number', 40003, 400);
}

return this.increment(-amount);
Expand All @@ -163,7 +163,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
if (op.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Cannot apply state operation with objectId=${op.objectId}, to this LiveCounter with objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
default:
throw new this._client.ErrorInfo(
`Invalid ${op.action} op for LiveCounter objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand All @@ -226,7 +226,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
if (stateObject.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Invalid state object: state object objectId=${stateObject.objectId}; LiveCounter objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand All @@ -236,15 +236,15 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
if (stateObject.createOp.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Invalid state object: state object createOp objectId=${stateObject.createOp?.objectId}; LiveCounter objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}

if (stateObject.createOp.action !== StateOperationAction.COUNTER_CREATE) {
throw new this._client.ErrorInfo(
`Invalid state object: state object createOp action=${stateObject.createOp?.action}; LiveCounter objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand Down Expand Up @@ -308,7 +308,7 @@ export class LiveCounter extends LiveObject<LiveCounterData, LiveCounterUpdate>
private _throwNoPayloadError(op: StateOperation): void {
throw new this._client.ErrorInfo(
`No payload found for ${op.action} op for LiveCounter objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand Down
26 changes: 13 additions & 13 deletions src/plugins/liveobjects/livemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
const client = liveObjects.getClient();

if (typeof key !== 'string') {
throw new client.ErrorInfo('Map key should be string', 40013, 400);
throw new client.ErrorInfo('Map key should be string', 40003, 400);
}

const stateMessage = StateMessage.fromValues(
Expand Down Expand Up @@ -172,7 +172,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
const client = liveObjects.getClient();

if (typeof key !== 'string') {
throw new client.ErrorInfo('Map key should be string', 40013, 400);
throw new client.ErrorInfo('Map key should be string', 40003, 400);
}

if (
Expand All @@ -193,7 +193,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
const client = liveObjects.getClient();

if (entries !== undefined && (entries === null || typeof entries !== 'object')) {
throw new client.ErrorInfo('Map entries should be a key/value object', 40013, 400);
throw new client.ErrorInfo('Map entries should be a key/value object', 40003, 400);
}

Object.entries(entries ?? {}).forEach(([key, value]) => LiveMap.validateKeyValue(liveObjects, key, value));
Expand Down Expand Up @@ -368,7 +368,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
if (op.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Cannot apply state operation with objectId=${op.objectId}, to this LiveMap with objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand Down Expand Up @@ -426,7 +426,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
default:
throw new this._client.ErrorInfo(
`Invalid ${op.action} op for LiveMap objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand All @@ -441,15 +441,15 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
if (stateObject.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Invalid state object: state object objectId=${stateObject.objectId}; LiveMap objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}

if (stateObject.map?.semantics !== this._semantics) {
throw new this._client.ErrorInfo(
`Invalid state object: state object map semantics=${stateObject.map?.semantics}; LiveMap semantics=${this._semantics}`,
50000,
92000,
500,
);
}
Expand All @@ -459,23 +459,23 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
if (stateObject.createOp.objectId !== this.getObjectId()) {
throw new this._client.ErrorInfo(
`Invalid state object: state object createOp objectId=${stateObject.createOp?.objectId}; LiveMap objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}

if (stateObject.createOp.action !== StateOperationAction.MAP_CREATE) {
throw new this._client.ErrorInfo(
`Invalid state object: state object createOp action=${stateObject.createOp?.action}; LiveMap objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}

if (stateObject.createOp.map?.semantics !== this._semantics) {
throw new this._client.ErrorInfo(
`Invalid state object: state object createOp map semantics=${stateObject.createOp.map?.semantics}; LiveMap semantics=${this._semantics}`,
50000,
92000,
500,
);
}
Expand Down Expand Up @@ -621,7 +621,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
private _throwNoPayloadError(op: StateOperation): void {
throw new this._client.ErrorInfo(
`No payload found for ${op.action} op for LiveMap objectId=${this.getObjectId()}`,
50000,
92000,
500,
);
}
Expand All @@ -643,7 +643,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
if (this._semantics !== op.map?.semantics) {
throw new this._client.ErrorInfo(
`Cannot apply MAP_CREATE op on LiveMap objectId=${this.getObjectId()}; map's semantics=${this._semantics}, but op expected ${op.map?.semantics}`,
50000,
92000,
500,
);
}
Expand All @@ -669,7 +669,7 @@ export class LiveMap<T extends API.LiveMapType> extends LiveObject<LiveMapData,
if (Utils.isNil(op.data) || (Utils.isNil(op.data.value) && Utils.isNil(op.data.objectId))) {
throw new ErrorInfo(
`Invalid state data for MAP_SET op on objectId=${this.getObjectId()} on key=${op.key}`,
50000,
92000,
500,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/liveobjects/liveobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ export abstract class LiveObject<
*/
protected _canApplyOperation(opOriginTimeserial: string | undefined, opSiteCode: string | undefined): boolean {
if (!opOriginTimeserial) {
throw new this._client.ErrorInfo(`Invalid timeserial: ${opOriginTimeserial}`, 50000, 500);
throw new this._client.ErrorInfo(`Invalid timeserial: ${opOriginTimeserial}`, 92000, 500);
}

if (!opSiteCode) {
throw new this._client.ErrorInfo(`Invalid site code: ${opSiteCode}`, 50000, 500);
throw new this._client.ErrorInfo(`Invalid site code: ${opSiteCode}`, 92000, 500);
}

const siteTimeserial = this._siteTimeserials[opSiteCode];
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/liveobjects/liveobjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,10 @@ export class LiveObjects {
// channel.modes is only populated on channel attachment, so use it only if it is set,
// otherwise as a best effort use user provided channel options
if (this._channel.modes != null && !this._channel.modes.includes(expectedMode)) {
throw new this._client.ErrorInfo(`"${expectedMode}" channel mode must be set for this operation`, 40160, 400);
throw new this._client.ErrorInfo(`"${expectedMode}" channel mode must be set for this operation`, 40024, 400);
}
if (!this._client.Utils.allToLowerCase(this._channel.channelOptions.modes ?? []).includes(expectedMode)) {
throw new this._client.ErrorInfo(`"${expectedMode}" channel mode must be set for this operation`, 40160, 400);
throw new this._client.ErrorInfo(`"${expectedMode}" channel mode must be set for this operation`, 40024, 400);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/liveobjects/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ export class ObjectId {
*/
static fromString(client: BaseClient, objectId: string | null | undefined): ObjectId {
if (client.Utils.isNil(objectId)) {
throw new client.ErrorInfo('Invalid object id string', 50000, 500);
throw new client.ErrorInfo('Invalid object id string', 92000, 500);
}

const [type, rest] = objectId.split(':');
if (!type || !rest) {
throw new client.ErrorInfo('Invalid object id string', 50000, 500);
throw new client.ErrorInfo('Invalid object id string', 92000, 500);
}

if (!['map', 'counter'].includes(type)) {
throw new client.ErrorInfo(`Invalid object type in object id: ${objectId}`, 50000, 500);
throw new client.ErrorInfo(`Invalid object type in object id: ${objectId}`, 92000, 500);
}

const [hash, msTimestamp] = rest.split('@');
if (!hash || !msTimestamp) {
throw new client.ErrorInfo('Invalid object id string', 50000, 500);
throw new client.ErrorInfo('Invalid object id string', 92000, 500);
}

if (!Number.isInteger(Number.parseInt(msTimestamp))) {
throw new client.ErrorInfo('Invalid object id string', 50000, 500);
throw new client.ErrorInfo('Invalid object id string', 92000, 500);
}

return new ObjectId(type as LiveObjectType, hash, Number.parseInt(msTimestamp));
Expand Down

0 comments on commit 6fc1702

Please sign in to comment.