Skip to content

Commit ee755d5

Browse files
committed
Merge branch 'release/1.4.8'
2 parents d344e51 + 65e2ecf commit ee755d5

File tree

7 files changed

+86
-78
lines changed

7 files changed

+86
-78
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.4.8 (2023-07-27 10:27)
2+
3+
### Fixed
4+
5+
* Fixed error return bug
6+
17
# 1.4.7 (2023-07-27 08:47)
28

39
### Fixed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.4.7",
3+
"version": "1.4.8",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/src/main.js",
66
"scripts": {

src/exceptions/401.exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class UnauthorizedException {
55
throw {
66
status: HttpStatus.UNAUTHORIZED,
77
error: 'Unauthorized',
8-
message: objectError.length > 0 ? objectError : undefined,
8+
message: objectError.length > 0 ? objectError : 'Unauthorized',
99
};
1010
}
1111
}

src/main.ts

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,76 @@ import { HttpStatus, router } from './whatsapp/routers/index.router';
1414
import { waMonitor } from './whatsapp/whatsapp.module';
1515

1616
function initWA() {
17-
waMonitor.loadInstance();
17+
waMonitor.loadInstance();
1818
}
1919

2020
function bootstrap() {
21-
const logger = new Logger('SERVER');
22-
const app = express();
23-
24-
app.use(
25-
cors({
26-
origin(requestOrigin, callback) {
27-
const { ORIGIN } = configService.get<Cors>('CORS');
28-
!requestOrigin ? (requestOrigin = '*') : undefined;
29-
if (ORIGIN.indexOf(requestOrigin) !== -1) {
30-
return callback(null, true);
31-
}
32-
return callback(new Error('Not allowed by CORS'));
33-
},
34-
methods: [...configService.get<Cors>('CORS').METHODS],
35-
credentials: configService.get<Cors>('CORS').CREDENTIALS,
36-
}),
37-
urlencoded({ extended: true, limit: '136mb' }),
38-
json({ limit: '136mb' }),
39-
compression(),
40-
);
41-
42-
app.set('view engine', 'hbs');
43-
app.set('views', join(ROOT_DIR, 'views'));
44-
app.use(express.static(join(ROOT_DIR, 'public')));
45-
46-
app.use('/', router);
47-
48-
app.use(
49-
(err: Error, req: Request, res: Response, next: NextFunction) => {
50-
if (err) {
51-
return res.status(err['status'] || 500).json({
52-
status: 'ERROR',
53-
error: err['error'] || 'Internal Server Error',
54-
response: {
55-
message: err['message'] || 'Internal Server Error',
56-
},
57-
}
58-
);
59-
}
60-
61-
next();
21+
const logger = new Logger('SERVER');
22+
const app = express();
23+
24+
app.use(
25+
cors({
26+
origin(requestOrigin, callback) {
27+
const { ORIGIN } = configService.get<Cors>('CORS');
28+
!requestOrigin ? (requestOrigin = '*') : undefined;
29+
if (ORIGIN.indexOf(requestOrigin) !== -1) {
30+
return callback(null, true);
31+
}
32+
return callback(new Error('Not allowed by CORS'));
33+
},
34+
methods: [...configService.get<Cors>('CORS').METHODS],
35+
credentials: configService.get<Cors>('CORS').CREDENTIALS,
36+
}),
37+
urlencoded({ extended: true, limit: '136mb' }),
38+
json({ limit: '136mb' }),
39+
compression(),
40+
);
41+
42+
app.set('view engine', 'hbs');
43+
app.set('views', join(ROOT_DIR, 'views'));
44+
app.use(express.static(join(ROOT_DIR, 'public')));
45+
46+
app.use('/', router);
47+
48+
app.use(
49+
(err: Error, req: Request, res: Response, next: NextFunction) => {
50+
if (err) {
51+
return res.status(err['status'] || 500).json({
52+
status: err['status'] || 500,
53+
error: err['error'] || 'Internal Server Error',
54+
response: {
55+
message: err['message'] || 'Internal Server Error',
56+
},
57+
});
58+
}
59+
60+
next();
61+
},
62+
(req: Request, res: Response, next: NextFunction) => {
63+
const { method, url } = req;
64+
65+
res.status(HttpStatus.NOT_FOUND).json({
66+
status: HttpStatus.NOT_FOUND,
67+
error: 'Not Found',
68+
response: {
69+
message: [`Cannot ${method.toUpperCase()} ${url}`],
6270
},
63-
(req: Request, res: Response, next: NextFunction) => {
64-
const { method, url } = req;
65-
66-
res.status(HttpStatus.NOT_FOUND).json({
67-
status: HttpStatus.NOT_FOUND,
68-
error: 'Not Found',
69-
response: {
70-
message: `Cannot ${method.toUpperCase()} ${url}`,
71-
},
72-
});
73-
74-
next();
75-
},
76-
);
71+
});
72+
73+
next();
74+
},
75+
);
7776

78-
const httpServer = configService.get<HttpServer>('SERVER');
77+
const httpServer = configService.get<HttpServer>('SERVER');
7978

80-
ServerUP.app = app;
81-
const server = ServerUP[httpServer.TYPE];
79+
ServerUP.app = app;
80+
const server = ServerUP[httpServer.TYPE];
8281

83-
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
82+
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
8483

85-
initWA();
84+
initWA();
8685

87-
onUnexpectedError();
86+
onUnexpectedError();
8887
}
8988

9089
bootstrap();

src/whatsapp/abstract/abstract.router.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,21 @@ export abstract class RouterBroker {
4848
const v = schema ? validate(ref, schema) : { valid: true, errors: [] };
4949

5050
if (!v.valid) {
51-
const message: any[] = v.errors.map(({ property, stack, schema }) => {
51+
const message: any[] = v.errors.map(({ stack, schema }) => {
5252
let message: string;
5353
if (schema['description']) {
5454
message = schema['description'];
5555
} else {
5656
message = stack.replace('instance.', '');
5757
}
58-
return {
59-
property: property.replace('instance.', ''),
60-
message,
61-
};
58+
return message;
59+
// return {
60+
// property: property.replace('instance.', ''),
61+
// message,
62+
// };
6263
});
63-
logger.error([...message]);
64-
throw new BadRequestException(...message);
64+
logger.error(message);
65+
throw new BadRequestException(message);
6566
}
6667

6768
return await execute(instance, ref);

src/whatsapp/controllers/instance.controller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ export class InstanceController {
220220
},
221221
};
222222
} catch (error) {
223-
console.log(error);
224-
return { error: true, message: error.toString() };
223+
this.logger.error(error.message[0]);
224+
throw new BadRequestException(error.message[0]);
225225
}
226226
}
227227

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

272-
return { error: false, message: 'Instance restarted' };
272+
return { status: 'SUCCESS', error: false, response: { message: 'Instance restarted' } };
273273
} catch (error) {
274274
this.logger.error(error);
275275
}
@@ -310,7 +310,7 @@ export class InstanceController {
310310
this.logger.verbose('close connection instance: ' + instanceName);
311311
this.waMonitor.waInstances[instanceName]?.client?.ws?.close();
312312

313-
return { error: false, message: 'Instance logged out' };
313+
return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
314314
} catch (error) {
315315
throw new InternalServerErrorException(error.toString());
316316
}
@@ -329,13 +329,13 @@ export class InstanceController {
329329

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

336336
delete this.waMonitor.waInstances[instanceName];
337337
this.eventEmitter.emit('remove.instance', instanceName, 'inner');
338-
return { error: false, message: 'Instance deleted' };
338+
return { status: 'SUCCESS', error: false, response: { message: 'Instance deleted' } };
339339
}
340340
} catch (error) {
341341
throw new BadRequestException(error.toString());

src/whatsapp/routers/instance.router.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ export class InstanceRouter extends RouterBroker {
161161
if (db.ENABLED) {
162162
try {
163163
await dbserver.dropDatabase();
164-
return res.status(HttpStatus.CREATED).json({ error: false, message: 'Database deleted' });
164+
return res
165+
.status(HttpStatus.CREATED)
166+
.json({ status: 'SUCCESS', error: false, response: { message: 'database deleted' } });
165167
} catch (error) {
166168
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ error: true, message: error.message });
167169
}

0 commit comments

Comments
 (0)