🐛 Fixes issue handling error responses in Rocketchat node (#1367)

This commit is contained in:
ricardo
2021-01-22 10:17:12 -05:00
parent cc4d6ab02a
commit 4ec458d56b
4 changed files with 114 additions and 100 deletions

View File

@@ -1,12 +1,14 @@
import { OptionsWithUri } from 'request';
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
export async function rocketchatApiRequest(this: IExecuteFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('rocketchatApi');
if (credentials === undefined) {
@@ -29,13 +31,15 @@ export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFuncti
try {
return await this.helpers.request!(options);
} catch (error) {
let errorMessage = error.message;
if (error.response && error.response.body && error.response.body.error) {
if (error.response.body.error) {
errorMessage = error.response.body.error;
const errorMessage = error.response.body.error;
// Try to return the error prettier
throw new Error(
`Rocketchat error response [${error.statusCode}]: ${errorMessage}`,
);
}
throw new Error(`Rocket.chat error response [${error.statusCode}]: ${errorMessage}`);
throw error;
}
}