@hmcts/one-per-page
Advanced tools
Comparing version 2.1.0-1 to 2.1.0-2
@@ -0,1 +1,10 @@ | ||
<a name="2.1.0-2"></a> | ||
# 2.1.0-2 (2018-01-09) | ||
* Ensure convert returns the wrapped fields mappedErrors instead of mapping it's own errors ([b1cd4b4](https://github.com/hmcts/one-per-page/commit/b1cd4b4)) | ||
* Fix linter errors ([2312cd4](https://github.com/hmcts/one-per-page/commit/2312cd4)) | ||
* Update yarn.lock ([ad68985](https://github.com/hmcts/one-per-page/commit/ad68985)) | ||
<a name="2.1.0-1"></a> | ||
@@ -2,0 +11,0 @@ # 2.1.0-1 (2017-12-22) |
const { Page } = require('@hmcts/one-per-page'); | ||
const thisIsAnExample = [ | ||
'This is an error that has been thrown on purpose to illustrate what', | ||
'happens in the event of an exception' | ||
].join(''); | ||
/** Class representing a what happens when an error is throw */ | ||
@@ -9,4 +14,4 @@ class ExampleServerError extends Page { | ||
handler(req, res) { | ||
throw 'This is an error that has been thrown on purpose to illustrate what happens in the event of an exception'; | ||
handler(/* req, res */) { | ||
throw new Error(thisIsAnExample); | ||
} | ||
@@ -13,0 +18,0 @@ } |
{ | ||
"name": "@hmcts/one-per-page", | ||
"description": "One question per page apps made easy", | ||
"version": "2.1.0-1", | ||
"version": "2.1.0-2", | ||
"main": "./src/main.js", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -7,7 +7,5 @@ // const { Page } = require('@hmcts/one-per-page'); | ||
const { loadFileContents } = require('../i18n/loadStepContent'); | ||
const { isArray, defined } = require('../util/checks'); | ||
class ErrorPages { | ||
constructor() { | ||
} | ||
/** bind 404 and 500's to the app */ | ||
@@ -17,6 +15,10 @@ static bind(app, userOpts) { | ||
path.join(__dirname, 'errorPages.content.en.json'), i18NextInstance) | ||
.then((i18Next) => { | ||
.then(i18Next => { | ||
const opts = userOpts || {}; | ||
// express requires "error handling" functions to accept 4 args | ||
// and uses that to identify it as an error handler. So we need to | ||
// declare next even though we don't use it. | ||
// | ||
// eslint-disable-next-line no-unused-vars | ||
app.use((errors, req, res, next) => { | ||
@@ -32,3 +34,4 @@ const serverError = opts.serverError || {}; | ||
'serverError.assets', | ||
{ path: req.app.locals.asset_path + 'main.css' }) | ||
{ path: `${req.app.locals.asset_path}main.css` } | ||
) | ||
} | ||
@@ -38,6 +41,5 @@ ); | ||
app.use((req, res, next) => { | ||
app.use((req, res) => { | ||
const notFound = opts.notFound || {}; | ||
if (typeof notFound.nextSteps !== 'undefined' | ||
&& !Array.isArray(notFound.nextSteps)) { | ||
if (defined(notFound.nextSteps) && !isArray(notFound.nextSteps)) { | ||
throw new TypeError('nextSteps is expected to be an array'); | ||
@@ -51,6 +53,8 @@ } | ||
nextSteps: notFound.nextSteps || i18Next.t( | ||
'notFound.nextSteps', { returnObjects: true }), | ||
'notFound.nextSteps', { returnObjects: true } | ||
), | ||
assets: notFound.assets || i18next.t( | ||
'notFound.assets', | ||
{ path: req.app.locals.asset_path + 'main.css' }) | ||
{ path: `${req.app.locals.asset_path}main.css` } | ||
) | ||
} | ||
@@ -57,0 +61,0 @@ ); |
const { notDefined, defined } = require('../util/checks'); | ||
const FieldError = require('./fieldError'); | ||
const { mapEntries, andWise } = require('../util/ops'); | ||
const { mapEntries, andWise, flattenArray } = require('../util/ops'); | ||
const { validator } = require('./validator'); | ||
@@ -60,3 +60,3 @@ | ||
if (serialized === {}) { | ||
return undefined; | ||
return undefined; // eslint-disable-line no-undefined | ||
} | ||
@@ -201,2 +201,8 @@ return serialized[this.name]; | ||
} | ||
get mappedErrors() { | ||
return [ | ||
this.wrapped.mappedErrors, | ||
super.errors.map(error => new FieldError(this, error)) | ||
].reduce(flattenArray); | ||
} | ||
} | ||
@@ -203,0 +209,0 @@ |
@@ -11,12 +11,11 @@ const { fileExists, readJson, glob } = require('../util/fs'); | ||
const translationMapper = (contents, filepath) => { | ||
return Object.keys(contents) | ||
.map(key => { | ||
if (defined(key.match(langRegex))) { | ||
return [{ lang: key, translations: contents[key] }]; | ||
} | ||
throw new Error(keyParseError(key, filepath)); | ||
}) | ||
.reduce((acc, arr) => [...acc, ...arr], []); | ||
}; | ||
const translationMapper = (contents, filepath) => Object | ||
.keys(contents) | ||
.map(key => { | ||
if (defined(key.match(langRegex))) { | ||
return [{ lang: key, translations: contents[key] }]; | ||
} | ||
throw new Error(keyParseError(key, filepath)); | ||
}) | ||
.reduce((acc, arr) => [...acc, ...arr], []); | ||
@@ -34,4 +33,4 @@ const parseI18N = (filepath, contents) => { | ||
const loadFileContents = (filePath, i18Next) => { | ||
const addResourceBundles = (contents, i18Next, filepath) => { | ||
const bundles = translationMapper(contents, filepath); | ||
const addResourceBundles = contents => { | ||
const bundles = translationMapper(contents, filePath); | ||
bundles.forEach(({ lang, translations }) => { | ||
@@ -53,5 +52,3 @@ const deep = true; | ||
.then(readJson) | ||
.then((contents) => { | ||
return addResourceBundles(contents, i18Next, filePath); | ||
}); | ||
.then(addResourceBundles); | ||
}; | ||
@@ -58,0 +55,0 @@ |
@@ -17,2 +17,4 @@ const notDefined = val => typeof val === 'undefined' || val === null; | ||
const isArray = maybeArray => defined(maybeArray) && Array.isArray(maybeArray); | ||
const isObject = maybeObj => | ||
@@ -27,5 +29,5 @@ typeof maybeObj === 'object' && !Array.isArray(maybeObj); | ||
notDefined, defined, | ||
ensureArray, | ||
isArray, ensureArray, | ||
hasKey, hasKeys, | ||
isObject, isEmptyObject | ||
}; |
@@ -1,4 +0,7 @@ | ||
const ErrorPages = require('../../src/errors/errorPages'); | ||
const { expect, sinon } = require('../util/chai'); | ||
const { supertest, testApp, wrapWithResponseAssertions } = require('../util/supertest'); | ||
const { expect } = require('../util/chai'); | ||
const { | ||
supertest, | ||
testApp, | ||
wrapWithResponseAssertions | ||
} = require('../util/supertest'); | ||
const { journey } = require('../../src/flow'); | ||
@@ -63,4 +66,3 @@ const { NOT_FOUND, INTERNAL_SERVER_ERROR } = require('http-status-codes'); | ||
app, '/some-random-page', NOT_FOUND, | ||
'could be because you\'ve followed a ' + | ||
'broken or outdated link, or there\'s an error on our site.' | ||
'could be because you\'ve followed a broken or outdated link' | ||
); | ||
@@ -102,7 +104,5 @@ }); | ||
}); | ||
}); | ||
describe('500 Server error', () => { | ||
const failurePage = class extends Page { | ||
@@ -112,4 +112,4 @@ get name() { | ||
} | ||
handler(req, res) { | ||
throw 'an error occurred :('; | ||
handler(/* req, res */) { | ||
throw new Error('an error occurred :('); | ||
} | ||
@@ -130,4 +130,7 @@ }; | ||
INTERNAL_SERVER_ERROR, | ||
'Sorry, we\'re having technical problems\n' + | ||
'Please try again in a few minutes.'); | ||
[ | ||
"Sorry, we're having technical problems", | ||
'Please try again in a few minutes.' | ||
].join('\n') | ||
); | ||
}); | ||
@@ -148,3 +151,5 @@ | ||
const app = journey(testApp(), options({ | ||
steps: [failurePage], errorPages: customServerError })); | ||
steps: [failurePage], | ||
errorPages: customServerError | ||
})); | ||
@@ -175,2 +180,2 @@ it('should contain default header', () => { | ||
}); | ||
}); | ||
}); |
@@ -134,2 +134,3 @@ const { expect, sinon } = require('../../util/chai'); | ||
}; | ||
field.mappedErrors = [new FieldError(field, 'from field')]; | ||
const transformedErr = validator('f', 'from transformed', () => false); | ||
@@ -143,3 +144,3 @@ const t = new TransformFieldValue({ | ||
expect(t.mappedErrors).to.eql([ | ||
new FieldError(t, 'from field'), | ||
new FieldError(field, 'from field'), | ||
new FieldError(t, 'from transformed') | ||
@@ -146,0 +147,0 @@ ]); |
@@ -146,12 +146,13 @@ const express = require('express'); | ||
execute(method) { | ||
const testExecution = supertestInstance(this)[method](this.step.path); | ||
execute(method, maybePath) { | ||
const path = defined(maybePath) ? maybePath : this.step.path; | ||
const testExecution = supertestInstance(this)[method](path); | ||
return wrapWithResponseAssertions(testExecution); | ||
} | ||
get() { | ||
return this.execute('get'); | ||
get(maybePath) { | ||
return this.execute('get', maybePath); | ||
} | ||
post() { | ||
const postRequest = this.execute('post'); | ||
post(maybePath) { | ||
const postRequest = this.execute('post', maybePath); | ||
if (Object.keys(this.body).length !== 0) { | ||
@@ -164,10 +165,10 @@ return postRequest | ||
} | ||
patch() { | ||
return this.execute('patch'); | ||
patch(maybePath) { | ||
return this.execute('patch', maybePath); | ||
} | ||
put() { | ||
return this.execute('put'); | ||
put(maybePath) { | ||
return this.execute('put', maybePath); | ||
} | ||
delete() { | ||
return this.execute('delete'); | ||
delete(maybePath) { | ||
return this.execute('delete', maybePath); | ||
} | ||
@@ -174,0 +175,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7878
602758