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