mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
Better Apollo error handling using apollo-errors (used for newsletter for now)
This commit is contained in:
parent
fb6f651dae
commit
a370cca0ce
7 changed files with 36 additions and 22 deletions
|
@ -12,6 +12,7 @@
|
|||
"dependencies": {
|
||||
"analytics-node": "^2.1.1",
|
||||
"apollo-client": "^1.0.1",
|
||||
"apollo-errors": "^1.4.0",
|
||||
"babel-runtime": "^6.18.0",
|
||||
"bcrypt": "^0.8.7",
|
||||
"body-parser": "^1.15.2",
|
||||
|
|
|
@ -32,11 +32,10 @@ class Newsletter extends PureComponent {
|
|||
const result = await this.props.addEmailNewsletter({email: data.email});
|
||||
this.successCallbackSubscription(result);
|
||||
} catch(error) {
|
||||
console.error(error); // eslint-disable-line no-console
|
||||
this.props.flash(
|
||||
this.context.intl.formatMessage(Utils.decodeIntlError(error)),
|
||||
"error"
|
||||
);
|
||||
const graphQLError = error.graphQLErrors[0];
|
||||
console.error(graphQLError); // eslint-disable-line no-console
|
||||
const message = this.context.intl.formatMessage({id: `newsletter.error_${this.state.error.name}`}, {message: this.state.error.message});
|
||||
this.props.flash(message, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import { makeExecutableSchema } from 'graphql-tools';
|
|||
import deepmerge from 'deepmerge';
|
||||
import OpticsAgent from 'optics-agent'
|
||||
import DataLoader from 'dataloader';
|
||||
import { formatError } from 'apollo-errors';
|
||||
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { check } from 'meteor/check';
|
||||
|
@ -111,6 +112,9 @@ const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
|
|||
options.context[collection.options.collectionName].loader = new DataLoader(ids => findByIds(collection, ids, options.context), { cache: true });
|
||||
});
|
||||
|
||||
// add error formatting from apollo-errors
|
||||
options.formatError = formatError;
|
||||
|
||||
return options;
|
||||
}));
|
||||
|
||||
|
|
8
packages/vulcan-newsletter/lib/modules/i18n.js
Normal file
8
packages/vulcan-newsletter/lib/modules/i18n.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { addStrings } from 'meteor/vulcan:core';
|
||||
|
||||
addStrings('en', {
|
||||
'newsletter.error_invalid_email': `Sorry, that doesn't look like a valid email.`,
|
||||
'newsletter.error_already_subscribed': `Sorry, it looks like you're already subscribed to the list.`,
|
||||
'newsletter.error_has_unsubscribed': `Sorry, it looks like you've previously unsubscribed from the list, and we're not able to re-subscribe you automatically.`,
|
||||
'newsletter.error_subscription_failed': `Sorry, your subscription failed ({message}).`,
|
||||
});
|
|
@ -3,6 +3,7 @@ import Newsletters from './collection.js';
|
|||
import './emails.js';
|
||||
import './custom_fields.js';
|
||||
import './fragments.js';
|
||||
import './i18n.js';
|
||||
|
||||
import '../components/NewsletterSubscribe.jsx';
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import moment from 'moment';
|
|||
import { getSetting } from 'meteor/vulcan:core';
|
||||
import Newsletters from '../../modules/collection.js';
|
||||
import MailChimpNPM from 'mailchimp';
|
||||
import { createError } from 'apollo-errors';
|
||||
|
||||
/*
|
||||
|
||||
|
@ -46,11 +47,18 @@ if (settings) {
|
|||
const subscribe = callSyncAPI('lists', 'subscribe', subscribeOptions);
|
||||
return {result: 'subscribed', ...subscribe};
|
||||
} catch (error) {
|
||||
// if the email is already in the Mailchimp list, no need to throw an error
|
||||
if (error.message === "214") {
|
||||
return {result: 'already-subscribed'};
|
||||
console.log(error)
|
||||
let name, message;
|
||||
if (error.code == 212) {
|
||||
name = 'has_unsubscribed';
|
||||
} else if (error.code == 214) {
|
||||
name = 'already_subscribed';
|
||||
} else {
|
||||
name = 'subscription_failed';
|
||||
message = error.message;
|
||||
}
|
||||
throw new Error("subscription-failed", error.message);
|
||||
const NewsletterError = createError(name, { message });
|
||||
throw new NewsletterError();
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -81,7 +89,8 @@ if (settings) {
|
|||
list_id: listId,
|
||||
subject: subject,
|
||||
from_email: fromEmail,
|
||||
from_name: fromName
|
||||
from_name: fromName,
|
||||
timewarp: true // NOTE: Sidebar only
|
||||
},
|
||||
content: {
|
||||
html: html,
|
||||
|
@ -95,7 +104,8 @@ if (settings) {
|
|||
console.log('// Newsletter created');
|
||||
// console.log(campaign)
|
||||
|
||||
const scheduledMoment = moment().utcOffset(0).add(1, 'hours');
|
||||
// NOTE: Sidebar only, schedule for next day at 9 AM
|
||||
const scheduledMoment = moment().add(1, 'day').startOf('day').hour(9);
|
||||
const scheduledTime = scheduledMoment.format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
const scheduleOptions = {
|
||||
|
|
|
@ -28,20 +28,11 @@ const resolver = {
|
|||
if (!user || !Users.options.mutations.edit.check(currentUser, user)) {
|
||||
throw new Error(Utils.encodeIntlError({id: "app.noPermission"}));
|
||||
}
|
||||
try {
|
||||
return Newsletters.subscribeUser(user, false);
|
||||
} catch (error) {
|
||||
const errorMessage = error.message.includes('subscription-failed') ? Utils.encodeIntlError({id: "newsletter.subscription_failed"}) : error.message
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
return Newsletters.subscribeUser(user, false);
|
||||
},
|
||||
addEmailNewsletter(root, {email}, context) {
|
||||
try {
|
||||
return Newsletters.subscribeEmail(email, true);
|
||||
} catch (error) {
|
||||
const errorMessage = error.message.includes('subscription-failed') ? Utils.encodeIntlError({id: "newsletter.subscription_failed"}) : error.message
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
return Newsletters.subscribeEmail(email, true);
|
||||
},
|
||||
removeUserNewsletter(root, { userId }, context) {
|
||||
const currentUser = context.currentUser;
|
||||
|
|
Loading…
Add table
Reference in a new issue