🐛 Fix Mautic-Node error reporting

This commit is contained in:
Jan Oberhauser
2020-02-10 14:04:15 -08:00
parent 1777f171bd
commit 05c64341e6
2 changed files with 48 additions and 49 deletions

View File

@@ -10,6 +10,25 @@ import {
import {
IDataObject,
} from 'n8n-workflow';
import { errors } from 'imap-simple';
interface OMauticErrorResponse {
errors: Array<{
conde: number;
message: string;
}>;
}
function getErrors(error: OMauticErrorResponse): string {
const returnErrors: string[] = [];
for (const errorItem of error.errors) {
returnErrors.push(errorItem.message);
}
return returnErrors.join(', ');
}
export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('mauticApi');
@@ -26,14 +45,19 @@ export async function mauticApiRequest(this: IHookFunctions | IExecuteFunctions
json: true
};
try {
return await this.helpers.request!(options);
} catch (err) {
const errorMessage = err.error || err.error.message;
const returnData = await this.helpers.request!(options);
if (errorMessage !== undefined) {
throw new Error(errorMessage);
if (returnData.error) {
// They seem to to sometimes return 200 status but still error.
throw new Error(getErrors(returnData));
}
throw err;
return returnData;
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
throw new Error('Mautic Error: ' + getErrors(error.response.body));
}
throw new Error(`Mautic Error: ${error.message}`);
}
}