mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -06:00
fix: Adjusts in return errors
This commit is contained in:
parent
38409d9336
commit
332ec69ee8
@ -5,7 +5,7 @@ export class UnauthorizedException {
|
|||||||
throw {
|
throw {
|
||||||
status: HttpStatus.UNAUTHORIZED,
|
status: HttpStatus.UNAUTHORIZED,
|
||||||
error: 'Unauthorized',
|
error: 'Unauthorized',
|
||||||
message: objectError.length > 0 ? objectError : undefined,
|
message: objectError.length > 0 ? objectError : 'Unauthorized',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
111
src/main.ts
111
src/main.ts
@ -14,77 +14,76 @@ 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({
|
return res.status(err['status'] || 500).json({
|
||||||
status: 'ERROR',
|
status: err['status'] || 500,
|
||||||
error: err['error'] || 'Internal Server Error',
|
error: err['error'] || 'Internal Server Error',
|
||||||
response: {
|
response: {
|
||||||
message: err['message'] || 'Internal Server Error',
|
message: err['message'] || 'Internal Server Error',
|
||||||
},
|
},
|
||||||
}
|
});
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
},
|
||||||
|
(req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const { method, url } = req;
|
||||||
|
|
||||||
|
res.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
status: HttpStatus.NOT_FOUND,
|
||||||
|
error: 'Not Found',
|
||||||
|
response: {
|
||||||
|
message: [`Cannot ${method.toUpperCase()} ${url}`],
|
||||||
},
|
},
|
||||||
(req: Request, res: Response, next: NextFunction) => {
|
});
|
||||||
const { method, url } = req;
|
|
||||||
|
|
||||||
res.status(HttpStatus.NOT_FOUND).json({
|
next();
|
||||||
status: HttpStatus.NOT_FOUND,
|
},
|
||||||
error: 'Not Found',
|
);
|
||||||
response: {
|
|
||||||
message: `Cannot ${method.toUpperCase()} ${url}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
next();
|
const httpServer = configService.get<HttpServer>('SERVER');
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const httpServer = configService.get<HttpServer>('SERVER');
|
ServerUP.app = app;
|
||||||
|
const server = ServerUP[httpServer.TYPE];
|
||||||
|
|
||||||
ServerUP.app = app;
|
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
|
||||||
const server = ServerUP[httpServer.TYPE];
|
|
||||||
|
|
||||||
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
|
initWA();
|
||||||
|
|
||||||
initWA();
|
onUnexpectedError();
|
||||||
|
|
||||||
onUnexpectedError();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
@ -48,20 +48,21 @@ export abstract class RouterBroker {
|
|||||||
const v = schema ? validate(ref, schema) : { valid: true, errors: [] };
|
const v = schema ? validate(ref, schema) : { valid: true, errors: [] };
|
||||||
|
|
||||||
if (!v.valid) {
|
if (!v.valid) {
|
||||||
const message: any[] = v.errors.map(({ property, stack, schema }) => {
|
const message: any[] = v.errors.map(({ stack, schema }) => {
|
||||||
let message: string;
|
let message: string;
|
||||||
if (schema['description']) {
|
if (schema['description']) {
|
||||||
message = schema['description'];
|
message = schema['description'];
|
||||||
} else {
|
} else {
|
||||||
message = stack.replace('instance.', '');
|
message = stack.replace('instance.', '');
|
||||||
}
|
}
|
||||||
return {
|
return message;
|
||||||
property: property.replace('instance.', ''),
|
// return {
|
||||||
message,
|
// property: property.replace('instance.', ''),
|
||||||
};
|
// message,
|
||||||
|
// };
|
||||||
});
|
});
|
||||||
logger.error([...message]);
|
logger.error(message);
|
||||||
throw new BadRequestException(...message);
|
throw new BadRequestException(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await execute(instance, ref);
|
return await execute(instance, ref);
|
||||||
|
@ -220,8 +220,8 @@ export class InstanceController {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
this.logger.error(error.message[0]);
|
||||||
return { error: true, message: error.toString() };
|
throw new BadRequestException(error.message[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,7 +269,7 @@ export class InstanceController {
|
|||||||
this.logger.verbose('logging out instance: ' + instanceName);
|
this.logger.verbose('logging out instance: ' + instanceName);
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||||
|
|
||||||
return { error: false, message: 'Instance restarted' };
|
return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
}
|
}
|
||||||
@ -310,7 +310,7 @@ export class InstanceController {
|
|||||||
this.logger.verbose('close connection instance: ' + instanceName);
|
this.logger.verbose('close connection instance: ' + instanceName);
|
||||||
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
|
||||||
|
|
||||||
return { error: false, message: 'Instance logged out' };
|
return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new InternalServerErrorException(error.toString());
|
throw new InternalServerErrorException(error.toString());
|
||||||
}
|
}
|
||||||
@ -329,13 +329,13 @@ export class InstanceController {
|
|||||||
|
|
||||||
await this.logout({ instanceName });
|
await this.logout({ instanceName });
|
||||||
delete this.waMonitor.waInstances[instanceName];
|
delete this.waMonitor.waInstances[instanceName];
|
||||||
return { error: false, message: 'Instance deleted' };
|
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('deleting instance: ' + instanceName);
|
this.logger.verbose('deleting instance: ' + instanceName);
|
||||||
|
|
||||||
delete this.waMonitor.waInstances[instanceName];
|
delete this.waMonitor.waInstances[instanceName];
|
||||||
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
|
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
|
||||||
return { error: false, message: 'Instance deleted' };
|
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new BadRequestException(error.toString());
|
throw new BadRequestException(error.toString());
|
||||||
|
@ -161,7 +161,9 @@ export class InstanceRouter extends RouterBroker {
|
|||||||
if (db.ENABLED) {
|
if (db.ENABLED) {
|
||||||
try {
|
try {
|
||||||
await dbserver.dropDatabase();
|
await dbserver.dropDatabase();
|
||||||
return res.status(HttpStatus.CREATED).json({ error: false, message: 'Database deleted' });
|
return res
|
||||||
|
.status(HttpStatus.CREATED)
|
||||||
|
.json({ status: 'SUCCESS', error: false, response: { message: 'database deleted' } });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: true, message: error.message });
|
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: true, message: error.message });
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user