1a9b605dc7f45940c8f50a12cda54ab90d0b2557
[browser-rts.git] / src / errors.ts
1 export class HttpError extends Error {
2         errorCode: number;
3         statusCode: number;
4         constructor(msg: string, statusCode: number, errorCode: number) {
5                 super(msg);
6                 this.statusCode = statusCode;
7                 this.errorCode = errorCode;
8         }
9 }
10
11 export class NotFoundError extends HttpError {
12         constructor(msg: string, errorCode: number) {
13                 super(msg, 404, errorCode);
14         }
15 }
16
17 export class DuplicateContentError extends HttpError {
18         constructor(msg: string, errorCode: number) {
19                 super(msg, 409, errorCode);
20         }
21 }
22
23 export class BadInputError extends HttpError {
24         constructor(msg: string, errorCode: number) {
25                 super(msg, 400, errorCode);
26         }
27 }
28
29 export class InsufficientResourceError extends HttpError {
30         constructor(resource: string, expected: number, available: number) {
31                 super(
32                         `Insufficent ${resource}. Expected: ${expected}, only ${available} available`, 
33                         400,
34                         ERROR_CODE.INSUFFICIENT_RESOURCE
35                 );
36         }
37 }
38
39 export const ERROR_CODE = {
40         USER_NOT_FOUND: 1000,
41         USER_ALREADY_EXISTS: 1001,
42         USER_PASSWORD_INCORRECT: 1002,
43         INVALID_USERNAME: 1003,
44         INVALID_PASSWORD: 1004,
45         INVALID_USER_TOKEN: 1005,
46         USING_ANOTHER_USERS_TOKEN: 1006,
47         NO_CITY: 2000,
48         INSUFFICIENT_RESOURCE: 3000,
49         INVALID_BUILDING: 4000,
50   INVALID_AMOUNT: 6000,
51         INVALID_UNIT: 5000,
52         DUPLICATE_CACHE_KEY: 900,
53 }