mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
🐛 Fix Mautic-Node error reporting
This commit is contained in:
parent
1777f171bd
commit
05c64341e6
|
@ -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}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -68,12 +68,7 @@ export class Mautic implements INodeType {
|
|||
// select them easily
|
||||
async getCompanies(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
let companies;
|
||||
try {
|
||||
companies = await mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies');
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
}
|
||||
const companies = await mauticApiRequestAllItems.call(this, 'companies', 'GET', '/companies');
|
||||
for (const company of companies) {
|
||||
returnData.push({
|
||||
name: company.fields.all.companyname,
|
||||
|
@ -129,12 +124,8 @@ export class Mautic implements INodeType {
|
|||
if (additionalFields.ownerId) {
|
||||
body.ownerId = additionalFields.ownerId as string;
|
||||
}
|
||||
try {
|
||||
responseData = await mauticApiRequest.call(this, 'POST', '/contacts/new', body);
|
||||
responseData = responseData.contact;
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
}
|
||||
responseData = await mauticApiRequest.call(this, 'POST', '/contacts/new', body);
|
||||
responseData = responseData.contact;
|
||||
}
|
||||
//https://developer.mautic.org/?php#edit-contact
|
||||
if (operation === 'update') {
|
||||
|
@ -176,22 +167,14 @@ export class Mautic implements INodeType {
|
|||
if (updateFields.ownerId) {
|
||||
body.ownerId = updateFields.ownerId as string;
|
||||
}
|
||||
try {
|
||||
responseData = await mauticApiRequest.call(this, 'PATCH', `/contacts/${contactId}/edit`, body);
|
||||
responseData = responseData.contact;
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
}
|
||||
responseData = await mauticApiRequest.call(this, 'PATCH', `/contacts/${contactId}/edit`, body);
|
||||
responseData = responseData.contact;
|
||||
}
|
||||
//https://developer.mautic.org/?php#get-contact
|
||||
if (operation === 'get') {
|
||||
const contactId = this.getNodeParameter('contactId', i) as string;
|
||||
try {
|
||||
responseData = await mauticApiRequest.call(this, 'GET', `/contacts/${contactId}`);
|
||||
responseData = responseData.contact;
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
}
|
||||
responseData = await mauticApiRequest.call(this, 'GET', `/contacts/${contactId}`);
|
||||
responseData = responseData.contact;
|
||||
}
|
||||
//https://developer.mautic.org/?php#list-contacts
|
||||
if (operation === 'getAll') {
|
||||
|
@ -204,30 +187,22 @@ export class Mautic implements INodeType {
|
|||
qs.orderBy = snakeCase(qs.orderBy as string);
|
||||
}
|
||||
|
||||
try {
|
||||
if (returnAll === true) {
|
||||
responseData = await mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i) as number;
|
||||
qs.start = 0;
|
||||
responseData = await mauticApiRequest.call(this, 'GET', '/contacts', {}, qs);
|
||||
responseData = responseData.contacts;
|
||||
responseData = Object.values(responseData);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
if (returnAll === true) {
|
||||
responseData = await mauticApiRequestAllItems.call(this, 'contacts', 'GET', '/contacts', {}, qs);
|
||||
} else {
|
||||
qs.limit = this.getNodeParameter('limit', i) as number;
|
||||
qs.start = 0;
|
||||
responseData = await mauticApiRequest.call(this, 'GET', '/contacts', {}, qs);
|
||||
responseData = responseData.contacts;
|
||||
responseData = Object.values(responseData);
|
||||
}
|
||||
|
||||
}
|
||||
//https://developer.mautic.org/?php#delete-contact
|
||||
if (operation === 'delete') {
|
||||
const contactId = this.getNodeParameter('contactId', i) as string;
|
||||
try {
|
||||
responseData = await mauticApiRequest.call(this, 'DELETE', `/contacts/${contactId}/delete`);
|
||||
responseData = responseData.contact;
|
||||
} catch (err) {
|
||||
throw new Error(`Mautic Error: ${JSON.stringify(err)}`);
|
||||
}
|
||||
responseData = await mauticApiRequest.call(this, 'DELETE', `/contacts/${contactId}/delete`);
|
||||
responseData = responseData.contact;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue