Skip to content

Commit

Permalink
Merge branch 'release/1.4.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Jul 27, 2023
2 parents d344e51 + 65e2ecf commit ee755d5
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.8 (2023-07-27 10:27)

### Fixed

* Fixed error return bug

# 1.4.7 (2023-07-27 08:47)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.4.7",
"version": "1.4.8",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions/401.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class UnauthorizedException {
throw {
status: HttpStatus.UNAUTHORIZED,
error: 'Unauthorized',
message: objectError.length > 0 ? objectError : undefined,
message: objectError.length > 0 ? objectError : 'Unauthorized',
};
}
}
123 changes: 61 additions & 62 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,77 +14,76 @@ import { HttpStatus, router } from './whatsapp/routers/index.router';
import { waMonitor } from './whatsapp/whatsapp.module';

function initWA() {
waMonitor.loadInstance();
waMonitor.loadInstance();
}

function bootstrap() {
const logger = new Logger('SERVER');
const app = express();

app.use(
cors({
origin(requestOrigin, callback) {
const { ORIGIN } = configService.get<Cors>('CORS');
!requestOrigin ? (requestOrigin = '*') : undefined;
if (ORIGIN.indexOf(requestOrigin) !== -1) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
},
methods: [...configService.get<Cors>('CORS').METHODS],
credentials: configService.get<Cors>('CORS').CREDENTIALS,
}),
urlencoded({ extended: true, limit: '136mb' }),
json({ limit: '136mb' }),
compression(),
);

app.set('view engine', 'hbs');
app.set('views', join(ROOT_DIR, 'views'));
app.use(express.static(join(ROOT_DIR, 'public')));

app.use('/', router);

app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => {
if (err) {
return res.status(err['status'] || 500).json({
status: 'ERROR',
error: err['error'] || 'Internal Server Error',
response: {
message: err['message'] || 'Internal Server Error',
},
}
);
}

next();
const logger = new Logger('SERVER');
const app = express();

app.use(
cors({
origin(requestOrigin, callback) {
const { ORIGIN } = configService.get<Cors>('CORS');
!requestOrigin ? (requestOrigin = '*') : undefined;
if (ORIGIN.indexOf(requestOrigin) !== -1) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
},
methods: [...configService.get<Cors>('CORS').METHODS],
credentials: configService.get<Cors>('CORS').CREDENTIALS,
}),
urlencoded({ extended: true, limit: '136mb' }),
json({ limit: '136mb' }),
compression(),
);

app.set('view engine', 'hbs');
app.set('views', join(ROOT_DIR, 'views'));
app.use(express.static(join(ROOT_DIR, 'public')));

app.use('/', router);

app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => {
if (err) {
return res.status(err['status'] || 500).json({
status: err['status'] || 500,
error: err['error'] || 'Internal Server Error',
response: {
message: err['message'] || 'Internal Server Error',
},
});
}

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({
status: HttpStatus.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;
const server = ServerUP[httpServer.TYPE];
ServerUP.app = app;
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();
15 changes: 8 additions & 7 deletions src/whatsapp/abstract/abstract.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ export abstract class RouterBroker {
const v = schema ? validate(ref, schema) : { valid: true, errors: [] };

if (!v.valid) {
const message: any[] = v.errors.map(({ property, stack, schema }) => {
const message: any[] = v.errors.map(({ stack, schema }) => {
let message: string;
if (schema['description']) {
message = schema['description'];
} else {
message = stack.replace('instance.', '');
}
return {
property: property.replace('instance.', ''),
message,
};
return message;
// return {
// property: property.replace('instance.', ''),
// message,
// };
});
logger.error([...message]);
throw new BadRequestException(...message);
logger.error(message);
throw new BadRequestException(message);
}

return await execute(instance, ref);
Expand Down
12 changes: 6 additions & 6 deletions src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export class InstanceController {
},
};
} catch (error) {
console.log(error);
return { error: true, message: error.toString() };
this.logger.error(error.message[0]);
throw new BadRequestException(error.message[0]);
}
}

Expand Down Expand Up @@ -269,7 +269,7 @@ export class InstanceController {
this.logger.verbose('logging out instance: ' + instanceName);
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();

return { error: false, message: 'Instance restarted' };
return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } };
} catch (error) {
this.logger.error(error);
}
Expand Down Expand Up @@ -310,7 +310,7 @@ export class InstanceController {
this.logger.verbose('close connection instance: ' + instanceName);
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) {
throw new InternalServerErrorException(error.toString());
}
Expand All @@ -329,13 +329,13 @@ export class InstanceController {

await this.logout({ instanceName });
delete this.waMonitor.waInstances[instanceName];
return { error: false, message: 'Instance deleted' };
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
} else {
this.logger.verbose('deleting instance: ' + instanceName);

delete this.waMonitor.waInstances[instanceName];
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
return { error: false, message: 'Instance deleted' };
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
}
} catch (error) {
throw new BadRequestException(error.toString());
Expand Down
4 changes: 3 additions & 1 deletion src/whatsapp/routers/instance.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ export class InstanceRouter extends RouterBroker {
if (db.ENABLED) {
try {
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) {
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: true, message: error.message });
}
Expand Down

0 comments on commit ee755d5

Please sign in to comment.