sprinklers3/common/ErrorCode.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-06-25 17:37:36 -06:00
export enum ErrorCode {
BadRequest = 100,
NotSpecified = 101,
Parse = 102,
Range = 103,
InvalidData = 104,
BadToken = 105,
Unauthorized = 106,
NoPermission = 107,
NotImplemented = 108,
NotFound = 109,
2018-06-25 17:37:36 -06:00
Internal = 200,
Timeout = 300,
2018-06-27 00:59:58 -06:00
ServerDisconnected = 301,
BrokerDisconnected = 302,
2018-06-25 17:37:36 -06:00
}
export function toHttpStatus(errorCode: ErrorCode): number {
switch (errorCode) {
case ErrorCode.BadRequest:
case ErrorCode.NotSpecified:
case ErrorCode.Parse:
case ErrorCode.Range:
case ErrorCode.InvalidData:
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;
}
}