165 lines
3.5 KiB
TypeScript
165 lines
3.5 KiB
TypeScript
import { HttpException, HttpStatus } from "@nestjs/common";
|
|
import { randomUUID } from "crypto";
|
|
import * as js2xmlparser from 'js2xmlparser';
|
|
|
|
export abstract class AwsException {
|
|
|
|
requestId: string = randomUUID();
|
|
|
|
constructor(
|
|
readonly message: string,
|
|
readonly errorType: string,
|
|
readonly statusCode: HttpStatus,
|
|
) {}
|
|
|
|
toXml(): string {
|
|
return js2xmlparser.parse(`ErrorResponse`, {
|
|
RequestId: this.requestId,
|
|
Error: {
|
|
Code: this.errorType,
|
|
Message: this.message,
|
|
}
|
|
});
|
|
}
|
|
|
|
toJson(): [HttpException, Record<string, string>] {
|
|
return [
|
|
new HttpException({
|
|
message: this.message,
|
|
__type: this.errorType,
|
|
}, this.statusCode),
|
|
{
|
|
'Server': 'NestJS/local-aws',
|
|
'X-Amzn-Errortype': this.errorType,
|
|
'x-amzn-requestid': this.requestId,
|
|
}
|
|
];
|
|
}
|
|
}
|
|
|
|
export class AccessDeniedException extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
AccessDeniedException.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class IncompleteSignature extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
IncompleteSignature.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class InternalFailure extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
InternalFailure.name,
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
)
|
|
}
|
|
}
|
|
export class InvalidAction extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
InvalidAction.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class InvalidClientTokenId extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
InvalidClientTokenId.name,
|
|
HttpStatus.FORBIDDEN,
|
|
)
|
|
}
|
|
}
|
|
export class NotAuthorized extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
NotAuthorized.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class OptInRequired extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
OptInRequired.name,
|
|
HttpStatus.FORBIDDEN,
|
|
)
|
|
}
|
|
}
|
|
export class RequestExpired extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
RequestExpired.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class ServiceUnavailable extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
ServiceUnavailable.name,
|
|
HttpStatus.SERVICE_UNAVAILABLE,
|
|
)
|
|
}
|
|
}
|
|
export class ThrottlingException extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
ThrottlingException.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class ValidationError extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
ValidationError.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class NotFoundException extends AwsException {
|
|
constructor() {
|
|
super(
|
|
'The request was rejected because the specified entity or resource could not be found.',
|
|
NotFoundException.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class InvalidArnException extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
InvalidArnException.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
}
|
|
export class UnsupportedOperationException extends AwsException {
|
|
constructor(message: string) {
|
|
super(
|
|
message,
|
|
UnsupportedOperationException.name,
|
|
HttpStatus.BAD_REQUEST,
|
|
)
|
|
}
|
|
} |