fix: Adjusts in return errors

This commit is contained in:
Davidson Gomes 2023-07-27 08:51:58 -03:00
parent f74a7e87bd
commit f95d938fb6
3 changed files with 56 additions and 49 deletions

View File

@ -2,7 +2,6 @@ import { HttpStatus } from '../whatsapp/routers/index.router';
export class BadRequestException { export class BadRequestException {
constructor(...objectError: any[]) { constructor(...objectError: any[]) {
console.log('BadRequestException', objectError);
throw { throw {
status: HttpStatus.BAD_REQUEST, status: HttpStatus.BAD_REQUEST,
error: 'Bad Request', error: 'Bad Request',

View File

@ -14,68 +14,77 @@ import { HttpStatus, router } from './whatsapp/routers/index.router';
import { waMonitor } from './whatsapp/whatsapp.module'; import { waMonitor } from './whatsapp/whatsapp.module';
function initWA() { function initWA() {
waMonitor.loadInstance(); waMonitor.loadInstance();
} }
function bootstrap() { function bootstrap() {
const logger = new Logger('SERVER'); const logger = new Logger('SERVER');
const app = express(); const app = express();
app.use( app.use(
cors({ cors({
origin(requestOrigin, callback) { origin(requestOrigin, callback) {
const { ORIGIN } = configService.get<Cors>('CORS'); const { ORIGIN } = configService.get<Cors>('CORS');
!requestOrigin ? (requestOrigin = '*') : undefined; !requestOrigin ? (requestOrigin = '*') : undefined;
if (ORIGIN.indexOf(requestOrigin) !== -1) { if (ORIGIN.indexOf(requestOrigin) !== -1) {
return callback(null, true); return callback(null, true);
} }
return callback(new Error('Not allowed by CORS')); return callback(new Error('Not allowed by CORS'));
}, },
methods: [...configService.get<Cors>('CORS').METHODS], methods: [...configService.get<Cors>('CORS').METHODS],
credentials: configService.get<Cors>('CORS').CREDENTIALS, credentials: configService.get<Cors>('CORS').CREDENTIALS,
}), }),
urlencoded({ extended: true, limit: '136mb' }), urlencoded({ extended: true, limit: '136mb' }),
json({ limit: '136mb' }), json({ limit: '136mb' }),
compression(), compression(),
); );
app.set('view engine', 'hbs'); app.set('view engine', 'hbs');
app.set('views', join(ROOT_DIR, 'views')); app.set('views', join(ROOT_DIR, 'views'));
app.use(express.static(join(ROOT_DIR, 'public'))); app.use(express.static(join(ROOT_DIR, 'public')));
app.use('/', router); app.use('/', router);
app.use( app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => { (err: Error, req: Request, res: Response, next: NextFunction) => {
if (err) { if (err) {
return res.status(err['status'] || 500).json(err); return res.status(err['status'] || 500).json({
} status: 'ERROR',
error: err['error'] || 'Internal Server Error',
response: {
message: err['message'] || 'Internal Server Error',
},
}
);
}
next(); next();
}, },
(req: Request, res: Response, next: NextFunction) => { (req: Request, res: Response, next: NextFunction) => {
const { method, url } = req; const { method, url } = req;
res.status(HttpStatus.NOT_FOUND).json({ res.status(HttpStatus.NOT_FOUND).json({
status: HttpStatus.NOT_FOUND, status: HttpStatus.NOT_FOUND,
message: `Cannot ${method.toUpperCase()} ${url}`, error: 'Not Found',
error: 'Not Found', response: {
}); message: `Cannot ${method.toUpperCase()} ${url}`,
},
});
next(); next();
}, },
); );
const httpServer = configService.get<HttpServer>('SERVER'); const httpServer = configService.get<HttpServer>('SERVER');
ServerUP.app = app; ServerUP.app = app;
const server = ServerUP[httpServer.TYPE]; const server = ServerUP[httpServer.TYPE];
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
initWA(); initWA();
onUnexpectedError(); onUnexpectedError();
} }
bootstrap(); bootstrap();

View File

@ -930,7 +930,6 @@ export class ChatwootService {
} }
public async receiveWebhook(instance: InstanceDto, body: any) { public async receiveWebhook(instance: InstanceDto, body: any) {
console.log(body);
try { try {
this.logger.verbose('receive webhook to chatwoot instance: ' + instance.instanceName); this.logger.verbose('receive webhook to chatwoot instance: ' + instance.instanceName);
const client = await this.clientCw(instance); const client = await this.clientCw(instance);