From 6418712c47cc6ed8b2170e9de8e015eeec00988b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 3 Nov 2018 21:47:46 -0700 Subject: [PATCH] Improve error handling --- app/scenes/Settings/Details.js | 7 ++++--- app/utils/ApiClient.js | 8 ++++++++ server/models/Team.js | 15 ++++++++++++--- server/utils/domains.js | 2 +- webpack.config.js | 3 ++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/scenes/Settings/Details.js b/app/scenes/Settings/Details.js index c8c6fb85b..927ca356f 100644 --- a/app/scenes/Settings/Details.js +++ b/app/scenes/Settings/Details.js @@ -51,7 +51,7 @@ class Details extends React.Component { }); this.props.ui.showToast('Settings saved', 'success'); } catch (err) { - this.props.ui.showToast('Could not save'); + this.props.ui.showToast(err.message); } }; @@ -127,14 +127,15 @@ class Details extends React.Component { {this.subdomain && ( - You will be able to access your wiki at{' '} + You will access your knowledgebase at{' '} {this.subdomain}.getoutline.com )} diff --git a/app/utils/ApiClient.js b/app/utils/ApiClient.js index 56353b4aa..ff2fcc4f5 100644 --- a/app/utils/ApiClient.js +++ b/app/utils/ApiClient.js @@ -68,6 +68,14 @@ class ApiClient { const error = {}; error.statusCode = response.status; error.response = response; + + try { + const data = await response.json(); + error.message = data.message || ''; + } catch (_err) { + // we're trying to parse an error so JSON may not be valid + } + throw error; }; diff --git a/server/models/Team.js b/server/models/Team.js index 27cc41180..4c7b6b34e 100644 --- a/server/models/Team.js +++ b/server/models/Team.js @@ -21,9 +21,18 @@ const Team = sequelize.define( allowNull: true, validate: { isLowercase: true, - is: [/^[a-z\d-]+$/, 'i'], - len: [4, 32], - notIn: [RESERVED_SUBDOMAINS], + is: { + args: [/^[a-z\d-]+$/, 'i'], + msg: 'Must be only alphanumeric and dashes', + }, + len: { + args: [4, 32], + msg: 'Must be between 4 and 32 characters', + }, + notIn: { + args: [RESERVED_SUBDOMAINS], + msg: 'You chose a restricted word, please try another.', + }, }, unique: true, }, diff --git a/server/utils/domains.js b/server/utils/domains.js index 733591cf7..d43f988a1 100644 --- a/server/utils/domains.js +++ b/server/utils/domains.js @@ -1,7 +1,7 @@ // @flow import parseDomain from 'parse-domain'; -export function stripSubdomain(hostname) { +export function stripSubdomain(hostname: string) { const parsed = parseDomain(hostname); if (parsed.tld) return `${parsed.domain}.${parsed.tld}`; return parsed.domain; diff --git a/webpack.config.js b/webpack.config.js index ee115e6e9..97967498c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -16,7 +16,8 @@ const definePlugin = new webpack.DefinePlugin({ DEPLOYMENT: JSON.stringify(process.env.DEPLOYMENT || 'hosted'), 'process.env': { URL: JSON.stringify(process.env.URL), - SLACK_KEY: JSON.stringify(process.env.SLACK_KEY) + SLACK_KEY: JSON.stringify(process.env.SLACK_KEY), + SUBDOMAINS_ENABLED: JSON.stringify(process.env.SUBDOMAINS_ENABLED) } });