@rafikidota/eidolon
Version:
Sometimes, the best way to solve your own problems is to help someone else.
203 lines • 9.14 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var RpcExceptionFilter_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RpcExceptionFilter = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const common_2 = require("@nestjs/common");
const common_3 = require("@nestjs/common");
const common_4 = require("@nestjs/common");
const common_5 = require("@nestjs/common");
const common_6 = require("@nestjs/common");
const common_7 = require("@nestjs/common");
const common_8 = require("@nestjs/common");
const common_9 = require("@nestjs/common");
const common_10 = require("@nestjs/common");
const common_11 = require("@nestjs/common");
const common_12 = require("@nestjs/common");
const common_13 = require("@nestjs/common");
const common_14 = require("@nestjs/common");
const common_15 = require("@nestjs/common");
const common_16 = require("@nestjs/common");
const common_17 = require("@nestjs/common");
const common_18 = require("@nestjs/common");
const common_19 = require("@nestjs/common");
const common_20 = require("@nestjs/common");
const common_21 = require("@nestjs/common");
const common_22 = require("@nestjs/common");
const common_23 = require("@nestjs/common");
const common_24 = require("@nestjs/common");
const DefaultMessage = 'Internal Server Error';
let RpcExceptionFilter = RpcExceptionFilter_1 = class RpcExceptionFilter extends core_1.BaseExceptionFilter {
constructor() {
super();
this.logger = new common_24.Logger(RpcExceptionFilter_1.name);
}
catch(exception, host) {
const ctx = host.switchToHttp();
const request = ctx.getRequest();
const data = {
message: null,
status: null,
};
const { response } = exception;
switch (true) {
case this.check(response): {
data.message = response.message;
data.status = response.statusCode ?? response.status;
break;
}
case this.check(exception): {
data.message = exception.message;
data.status = exception.status;
break;
}
default:
data.message = exception.message ?? DefaultMessage;
data.status = common_23.HttpStatus.INTERNAL_SERVER_ERROR;
break;
}
const { message, status } = data;
const { method, url, startTime } = request;
const endTime = Date.now();
const timestamp = endTime - startTime;
const logMessage = this.getLogMessage(message);
const log = `${method} ${url} ${status} - ${logMessage} - +${timestamp}ms`;
if ([
common_23.HttpStatus.INTERNAL_SERVER_ERROR,
common_23.HttpStatus.NOT_IMPLEMENTED,
common_23.HttpStatus.BAD_GATEWAY,
common_23.HttpStatus.SERVICE_UNAVAILABLE,
common_23.HttpStatus.GATEWAY_TIMEOUT,
common_23.HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
].includes(status)) {
this.logger.error(log);
}
if ([
common_23.HttpStatus.BAD_REQUEST,
common_23.HttpStatus.UNAUTHORIZED,
common_23.HttpStatus.PAYMENT_REQUIRED,
common_23.HttpStatus.FORBIDDEN,
common_23.HttpStatus.NOT_FOUND,
common_23.HttpStatus.METHOD_NOT_ALLOWED,
common_23.HttpStatus.NOT_ACCEPTABLE,
common_23.HttpStatus.PROXY_AUTHENTICATION_REQUIRED,
common_23.HttpStatus.REQUEST_TIMEOUT,
common_23.HttpStatus.CONFLICT,
common_23.HttpStatus.GONE,
common_23.HttpStatus.LENGTH_REQUIRED,
common_23.HttpStatus.PRECONDITION_FAILED,
common_23.HttpStatus.PAYLOAD_TOO_LARGE,
common_23.HttpStatus.URI_TOO_LONG,
common_23.HttpStatus.UNSUPPORTED_MEDIA_TYPE,
common_23.HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE,
common_23.HttpStatus.EXPECTATION_FAILED,
common_23.HttpStatus.I_AM_A_TEAPOT,
common_23.HttpStatus.MISDIRECTED,
common_23.HttpStatus.UNPROCESSABLE_ENTITY,
common_23.HttpStatus.FAILED_DEPENDENCY,
common_23.HttpStatus.PRECONDITION_REQUIRED,
common_23.HttpStatus.TOO_MANY_REQUESTS,
].includes(status)) {
this.logger.warn(log);
}
switch (status) {
case common_23.HttpStatus.BAD_REQUEST:
exception = new common_3.BadRequestException(message);
break;
case common_23.HttpStatus.UNAUTHORIZED:
exception = new common_4.UnauthorizedException(message);
break;
case common_23.HttpStatus.FORBIDDEN:
exception = new common_5.ForbiddenException(message);
break;
case common_23.HttpStatus.NOT_FOUND:
exception = new common_6.NotFoundException(message);
break;
case common_23.HttpStatus.METHOD_NOT_ALLOWED:
exception = new common_7.MethodNotAllowedException(message);
break;
case common_23.HttpStatus.NOT_ACCEPTABLE:
exception = new common_8.NotAcceptableException(message);
break;
case common_23.HttpStatus.REQUEST_TIMEOUT:
exception = new common_9.RequestTimeoutException(message);
break;
case common_23.HttpStatus.CONFLICT:
exception = new common_10.ConflictException(message);
break;
case common_23.HttpStatus.GONE:
exception = new common_11.GoneException(message);
break;
case common_23.HttpStatus.PRECONDITION_FAILED:
exception = new common_12.PreconditionFailedException(message);
break;
case common_23.HttpStatus.UNSUPPORTED_MEDIA_TYPE:
exception = new common_13.UnsupportedMediaTypeException(message);
break;
case common_23.HttpStatus.I_AM_A_TEAPOT:
exception = new common_14.ImATeapotException(message);
break;
case common_23.HttpStatus.MISDIRECTED:
exception = new common_15.MisdirectedException(message);
break;
case common_23.HttpStatus.UNPROCESSABLE_ENTITY:
exception = new common_16.UnprocessableEntityException(message);
break;
case common_23.HttpStatus.INTERNAL_SERVER_ERROR:
exception = new common_17.InternalServerErrorException(message);
break;
case common_23.HttpStatus.NOT_IMPLEMENTED:
exception = new common_18.NotImplementedException(message);
break;
case common_23.HttpStatus.BAD_GATEWAY:
exception = new common_20.BadGatewayException(message);
break;
case common_23.HttpStatus.SERVICE_UNAVAILABLE:
exception = new common_19.ServiceUnavailableException(message);
break;
case common_23.HttpStatus.GATEWAY_TIMEOUT:
exception = new common_21.GatewayTimeoutException(message);
break;
case common_23.HttpStatus.HTTP_VERSION_NOT_SUPPORTED:
exception = new common_22.HttpVersionNotSupportedException(message);
break;
default:
exception = new common_2.HttpException(message, status);
break;
}
super.catch(exception, host);
}
check(data) {
if (data) {
const { message, status, statusCode } = data;
const code = statusCode ?? status;
if (message && Object.values(common_23.HttpStatus).includes(code)) {
return true;
}
}
return false;
}
getLogMessage(message) {
if (Array.isArray(message)) {
const [first] = message;
return first;
}
return message;
}
};
exports.RpcExceptionFilter = RpcExceptionFilter;
exports.RpcExceptionFilter = RpcExceptionFilter = RpcExceptionFilter_1 = __decorate([
(0, common_1.Catch)(),
__metadata("design:paramtypes", [])
], RpcExceptionFilter);
//# sourceMappingURL=rpc.exception.filter.js.map