Skip to content

Commit c6c2961

Browse files
authored
feat: make AV.Error constructible and inheritable (#670)
1 parent 89a253c commit c6c2961

File tree

3 files changed

+97
-60
lines changed

3 files changed

+97
-60
lines changed

src/error.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@ const _ = require('underscore');
55
*/
66

77
function AVError(code, message) {
8-
const error = new Error(message);
9-
error.code = code;
10-
return error;
8+
if (new.target) {
9+
const error = new Error(message);
10+
Object.setPrototypeOf(error, Object.getPrototypeOf(this));
11+
error.code = code;
12+
return error;
13+
}
14+
return new AVError(code, message);
1115
}
1216

17+
AVError.prototype = Object.create(Error.prototype, {
18+
constructor: {
19+
value: Error,
20+
enumerable: false,
21+
writable: true,
22+
configurable: true,
23+
},
24+
});
25+
26+
Object.setPrototypeOf(AVError, Error);
27+
1328
_.extend(
1429
AVError,
1530
/** @lends AV.Error */ {

storage.d.ts

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,287 +1126,287 @@ export enum LeaderboardVersionChangeInterval {
11261126
MONTH,
11271127
}
11281128

1129-
export namespace Error {
1129+
export class Error {
1130+
code: number;
1131+
message: string;
1132+
rawMessage?: string;
1133+
1134+
constructor(code: number, message: string);
1135+
11301136
/**
11311137
* Error code indicating some error other than those enumerated here.
11321138
*/
1133-
export const OTHER_CAUSE: -1;
1139+
static OTHER_CAUSE: -1;
11341140

11351141
/**
11361142
* Error code indicating that something has gone wrong with the server.
11371143
* If you get this error code, it is AV's fault.
11381144
*/
1139-
export const INTERNAL_SERVER_ERROR: 1;
1145+
static INTERNAL_SERVER_ERROR: 1;
11401146

11411147
/**
11421148
* Error code indicating the connection to the AV servers failed.
11431149
*/
1144-
export const CONNECTION_FAILED: 100;
1150+
static CONNECTION_FAILED: 100;
11451151

11461152
/**
11471153
* Error code indicating the specified object doesn't exist.
11481154
*/
1149-
export const OBJECT_NOT_FOUND: 101;
1155+
static OBJECT_NOT_FOUND: 101;
11501156

11511157
/**
11521158
* Error code indicating you tried to query with a datatype that doesn't
11531159
* support it, like exact matching an array or object.
11541160
*/
1155-
export const INVALID_QUERY: 102;
1161+
static INVALID_QUERY: 102;
11561162

11571163
/**
11581164
* Error code indicating a missing or invalid classname. Classnames are
11591165
* case-sensitive. They must start with a letter, and a-zA-Z0-9_ are the
11601166
* only valid characters.
11611167
*/
1162-
export const INVALID_CLASS_NAME: 103;
1168+
static INVALID_CLASS_NAME: 103;
11631169

11641170
/**
11651171
* Error code indicating an unspecified object id.
11661172
*/
1167-
export const MISSING_OBJECT_ID: 104;
1173+
static MISSING_OBJECT_ID: 104;
11681174

11691175
/**
11701176
* Error code indicating an invalid key name. Keys are case-sensitive. They
11711177
* must start with a letter, and a-zA-Z0-9_ are the only valid characters.
11721178
*/
1173-
export const INVALID_KEY_NAME: 105;
1179+
static INVALID_KEY_NAME: 105;
11741180

11751181
/**
11761182
* Error code indicating a malformed pointer. You should not see this unless
11771183
* you have been mucking about changing internal AV code.
11781184
*/
1179-
export const INVALID_POINTER: 106;
1185+
static INVALID_POINTER: 106;
11801186

11811187
/**
11821188
* Error code indicating that badly formed JSON was received upstream. This
11831189
* either indicates you have done something unusual with modifying how
11841190
* things encode to JSON, or the network is failing badly.
11851191
*/
1186-
export const INVALID_JSON: 107;
1192+
static INVALID_JSON: 107;
11871193

11881194
/**
11891195
* Error code indicating that the feature you tried to access is only
11901196
* available internally for testing purposes.
11911197
*/
1192-
export const COMMAND_UNAVAILABLE: 108;
1198+
static COMMAND_UNAVAILABLE: 108;
11931199

11941200
/**
11951201
* You must call AV.initialize before using the AV library.
11961202
*/
1197-
export const NOT_INITIALIZED: 109;
1203+
static NOT_INITIALIZED: 109;
11981204

11991205
/**
12001206
* Error code indicating that a field was set to an inconsistent type.
12011207
*/
1202-
export const INCORRECT_TYPE: 111;
1208+
static INCORRECT_TYPE: 111;
12031209

12041210
/**
12051211
* Error code indicating an invalid channel name. A channel name is either
12061212
* an empty string (the broadcast channel) or contains only a-zA-Z0-9_
12071213
* characters.
12081214
*/
1209-
export const INVALID_CHANNEL_NAME: 112;
1215+
static INVALID_CHANNEL_NAME: 112;
12101216

12111217
/**
12121218
* Error code indicating that push is misconfigured.
12131219
*/
1214-
export const PUSH_MISCONFIGURED: 115;
1220+
static PUSH_MISCONFIGURED: 115;
12151221

12161222
/**
12171223
* Error code indicating that the object is too large.
12181224
*/
1219-
export const OBJECT_TOO_LARGE: 116;
1225+
static OBJECT_TOO_LARGE: 116;
12201226

12211227
/**
12221228
* Error code indicating that the operation isn't allowed for clients.
12231229
*/
1224-
export const OPERATION_FORBIDDEN: 119;
1230+
static OPERATION_FORBIDDEN: 119;
12251231

12261232
/**
12271233
* Error code indicating the result was not found in the cache.
12281234
*/
1229-
export const CACHE_MISS: 120;
1235+
static CACHE_MISS: 120;
12301236

12311237
/**
12321238
* Error code indicating that an invalid key was used in a nested
12331239
* JSONObject.
12341240
*/
1235-
export const INVALID_NESTED_KEY: 121;
1241+
static INVALID_NESTED_KEY: 121;
12361242

12371243
/**
12381244
* Error code indicating that an invalid filename was used for AVFile.
12391245
* A valid file name contains only a-zA-Z0-9_. characters and is between 1
12401246
* and 128 characters.
12411247
*/
1242-
export const INVALID_FILE_NAME: 122;
1248+
static INVALID_FILE_NAME: 122;
12431249

12441250
/**
12451251
* Error code indicating an invalid ACL was provided.
12461252
*/
1247-
export const INVALID_ACL: 123;
1253+
static INVALID_ACL: 123;
12481254

12491255
/**
12501256
* Error code indicating that the request timed out on the server. Typically
12511257
* this indicates that the request is too expensive to run.
12521258
*/
1253-
export const TIMEOUT: 124;
1259+
static TIMEOUT: 124;
12541260

12551261
/**
12561262
* Error code indicating that the email address was invalid.
12571263
*/
1258-
export const INVALID_EMAIL_ADDRESS: 125;
1264+
static INVALID_EMAIL_ADDRESS: 125;
12591265

12601266
/**
12611267
* Error code indicating a missing content type.
12621268
*/
1263-
export const MISSING_CONTENT_TYPE: 126;
1269+
static MISSING_CONTENT_TYPE: 126;
12641270

12651271
/**
12661272
* Error code indicating a missing content length.
12671273
*/
1268-
export const MISSING_CONTENT_LENGTH: 127;
1274+
static MISSING_CONTENT_LENGTH: 127;
12691275

12701276
/**
12711277
* Error code indicating an invalid content length.
12721278
*/
1273-
export const INVALID_CONTENT_LENGTH: 128;
1279+
static INVALID_CONTENT_LENGTH: 128;
12741280

12751281
/**
12761282
* Error code indicating a file that was too large.
12771283
*/
1278-
export const FILE_TOO_LARGE: 129;
1284+
static FILE_TOO_LARGE: 129;
12791285

12801286
/**
12811287
* Error code indicating an error saving a file.
12821288
*/
1283-
export const FILE_SAVE_ERROR: 130;
1289+
static FILE_SAVE_ERROR: 130;
12841290

12851291
/**
12861292
* Error code indicating an error deleting a file.
12871293
*/
1288-
export const FILE_DELETE_ERROR: 153;
1294+
static FILE_DELETE_ERROR: 153;
12891295

12901296
/**
12911297
* Error code indicating that a unique field was given a value that is
12921298
* already taken.
12931299
*/
1294-
export const DUPLICATE_VALUE: 137;
1300+
static DUPLICATE_VALUE: 137;
12951301

12961302
/**
12971303
* Error code indicating that a role's name is invalid.
12981304
*/
1299-
export const INVALID_ROLE_NAME: 139;
1305+
static INVALID_ROLE_NAME: 139;
13001306

13011307
/**
13021308
* Error code indicating that an application quota was exceeded. Upgrade to
13031309
* resolve.
13041310
*/
1305-
export const EXCEEDED_QUOTA: 140;
1311+
static EXCEEDED_QUOTA: 140;
13061312

13071313
/**
13081314
* Error code indicating that a Cloud Code script failed.
13091315
*/
1310-
export const SCRIPT_FAILED: 141;
1316+
static SCRIPT_FAILED: 141;
13111317

13121318
/**
13131319
* Error code indicating that a Cloud Code validation failed.
13141320
*/
1315-
export const VALIDATION_ERROR: 142;
1321+
static VALIDATION_ERROR: 142;
13161322

13171323
/**
13181324
* Error code indicating that invalid image data was provided.
13191325
*/
1320-
export const INVALID_IMAGE_DATA: 150;
1326+
static INVALID_IMAGE_DATA: 150;
13211327

13221328
/**
13231329
* Error code indicating an unsaved file.
13241330
*/
1325-
export const UNSAVED_FILE_ERROR: 151;
1331+
static UNSAVED_FILE_ERROR: 151;
13261332

13271333
/**
13281334
* Error code indicating an invalid push time.
13291335
*/
1330-
export const INVALID_PUSH_TIME_ERROR: 152;
1336+
static INVALID_PUSH_TIME_ERROR: 152;
13311337

13321338
/**
13331339
* Error code indicating that the username is missing or empty.
13341340
*/
1335-
export const USERNAME_MISSING: 200;
1341+
static USERNAME_MISSING: 200;
13361342

13371343
/**
13381344
* Error code indicating that the password is missing or empty.
13391345
*/
1340-
export const PASSWORD_MISSING: 201;
1346+
static PASSWORD_MISSING: 201;
13411347

13421348
/**
13431349
* Error code indicating that the username has already been taken.
13441350
*/
1345-
export const USERNAME_TAKEN: 202;
1351+
static USERNAME_TAKEN: 202;
13461352

13471353
/**
13481354
* Error code indicating that the email has already been taken.
13491355
*/
1350-
export const EMAIL_TAKEN: 203;
1356+
static EMAIL_TAKEN: 203;
13511357

13521358
/**
13531359
* Error code indicating that the email is missing, but must be specified.
13541360
*/
1355-
export const EMAIL_MISSING: 204;
1361+
static EMAIL_MISSING: 204;
13561362

13571363
/**
13581364
* Error code indicating that a user with the specified email was not found.
13591365
*/
1360-
export const EMAIL_NOT_FOUND: 205;
1366+
static EMAIL_NOT_FOUND: 205;
13611367

13621368
/**
13631369
* Error code indicating that a user object without a valid session could
13641370
* not be altered.
13651371
*/
1366-
export const SESSION_MISSING: 206;
1372+
static SESSION_MISSING: 206;
13671373

13681374
/**
13691375
* Error code indicating that a user can only be created through signup.
13701376
*/
1371-
export const MUST_CREATE_USER_THROUGH_SIGNUP: 207;
1377+
static MUST_CREATE_USER_THROUGH_SIGNUP: 207;
13721378

13731379
/**
13741380
* Error code indicating that an an account being linked is already linked
13751381
* to another user.
13761382
*/
1377-
export const ACCOUNT_ALREADY_LINKED: 208;
1383+
static ACCOUNT_ALREADY_LINKED: 208;
13781384

13791385
/**
13801386
* Error code indicating that a user cannot be linked to an account because
13811387
* that account's id could not be found.
13821388
*/
1383-
export const LINKED_ID_MISSING: 250;
1389+
static LINKED_ID_MISSING: 250;
13841390

13851391
/**
13861392
* Error code indicating that a user with a linked (e.g. Facebook) account
13871393
* has an invalid session.
13881394
*/
1389-
export const INVALID_LINKED_SESSION: 251;
1395+
static INVALID_LINKED_SESSION: 251;
13901396

13911397
/**
13921398
* Error code indicating that a service being linked (e.g. Facebook or
13931399
* Twitter) is unsupported.
13941400
*/
1395-
export const UNSUPPORTED_SERVICE: 252;
1401+
static UNSUPPORTED_SERVICE: 252;
13961402

13971403
/**
13981404
* Error code indicating a real error code is unavailable because
13991405
* we had to use an XDomainRequest object to allow CORS requests in
14001406
* Internet Explorer, which strips the body from HTTP responses that have
14011407
* a non-2XX status code.
14021408
*/
1403-
export const X_DOMAIN_REQUEST: 602;
1404-
}
1405-
1406-
export interface Error {
1407-
code: number;
1408-
message: string;
1409-
rawMessage?: string;
1409+
static X_DOMAIN_REQUEST: 602;
14101410
}
14111411

14121412
/**

0 commit comments

Comments
 (0)