-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #768 from digital-land/rosado/189-error-pages
This PR removes all the errorPages/xxx.html templates and replaces them with a single error template. It introduces a Error subclass MiddlewareError that should be use when we need to force the page to display specific error. The error pages include stack traces and (when relevant) schema validation issues. Both are displayed only on when the environment process.env.NODE_ENV || process.env.ENVIRONMENT is not set to production. Also, no errors details are shown on 404 page - it would be noise. Also: the templates for URL/file upload errors in the check tool provide /check link to start over (it used to be /url and /upload).
- Loading branch information
Showing
28 changed files
with
212 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,10 @@ email: { | |
validations: { | ||
maxFileSize: 100000000 | ||
} | ||
contact: | ||
issues: | ||
# description: should be used for general issues, e.g. if user lands on 404 doesn't know how to proceed | ||
email: [email protected] | ||
datasetsConfig: | ||
article-4-direction: | ||
guidanceUrl: /guidance/specifications/article-4-direction | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as v from 'valibot' | ||
|
||
/** | ||
* Takes an Error and if it's a ValiError, returns an array of issue summaries (with paths). Empty array otherwise. | ||
* | ||
* Useful to show the schema issues in a consise way. | ||
* | ||
* @param {Error} error | ||
* @returns { { path: string[], message: string}[] } | ||
*/ | ||
export function schemaIssues (error) { | ||
const issues = [] | ||
if (v.isValiError(error)) { | ||
for (const issue of error.issues) { | ||
if (issue.path) { | ||
issues.push({ path: issue.path.map(elem => elem.key), message: issue.message }) | ||
} | ||
} | ||
} | ||
return issues | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import config from '../../config/index.js' | ||
|
||
const context = { | ||
env: process.env.NODE_ENV || process.env.ENVIRONMENT || 'local', | ||
supportEmail: config.contact.issues.email | ||
} | ||
|
||
export function errorTemplateContext () { | ||
return { ...context } | ||
} | ||
|
||
/** | ||
* Use this class if you want to display specific HTTP error page. | ||
* | ||
* Uses the `errorPages/error.njk` template, but it can be overriden via options. | ||
*/ | ||
export class MiddlewareError extends Error { | ||
/** | ||
* @param {string} message | ||
* @param {number} statusCode status to be returned to the client | ||
* @param {{template?: string, cause?: Error}} options template path | ||
*/ | ||
constructor (message, statusCode, options = {}) { | ||
super(message, options) | ||
if (typeof statusCode !== 'number') { | ||
throw new Error(`statusCode should be a number: ${statusCode}`) | ||
} | ||
this.statusCode = statusCode | ||
this.template = options?.template ?? 'errorPages/error.njk' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.