sprinklers3/common/ErrorCode.ts

44 lines
1.0 KiB
TypeScript
Raw Normal View History

2018-06-25 17:37:36 -06:00
export enum ErrorCode {
2018-09-02 02:57:55 -06:00
BadRequest = 100,
NotSpecified = 101,
Parse = 102,
Range = 103,
InvalidData = 104,
BadToken = 105,
Unauthorized = 106,
NoPermission = 107,
NotFound = 109,
2018-09-04 14:46:26 -06:00
NotUnique = 110,
2018-09-02 02:57:55 -06:00
Internal = 200,
2018-09-04 14:46:26 -06:00
NotImplemented = 201,
2018-09-02 02:57:55 -06:00
Timeout = 300,
ServerDisconnected = 301,
BrokerDisconnected = 302
2018-06-25 17:37:36 -06:00
}
export function toHttpStatus(errorCode: ErrorCode): number {
2018-09-02 02:57:55 -06:00
switch (errorCode) {
case ErrorCode.BadRequest:
case ErrorCode.NotSpecified:
case ErrorCode.Parse:
case ErrorCode.Range:
case ErrorCode.InvalidData:
2018-09-04 14:46:26 -06:00
case ErrorCode.NotUnique:
2018-09-02 02:57:55 -06:00
return 400; // Bad request
case ErrorCode.Unauthorized:
case ErrorCode.BadToken:
return 401; // Unauthorized
case ErrorCode.NoPermission:
return 403; // Forbidden
case ErrorCode.NotFound:
return 404;
case ErrorCode.NotImplemented:
return 501;
case ErrorCode.Internal:
case ErrorCode.ServerDisconnected:
case ErrorCode.BrokerDisconnected:
default:
return 500;
}
}