Skip to content

Commit

Permalink
feat: added html-to-text support for API friendly responses
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed May 12, 2020
1 parent 3b4da8f commit 58a9571
Show file tree
Hide file tree
Showing 5 changed files with 1,658 additions and 1,507 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- '10'
- '12'
- 'node'
- 'lts/*'
after_success:
npm run coverage
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ try {
```


## API Friendly Messages

By default if `ctx.api` is true, then [html-to-text](https://github.com/werk85/node-html-to-text) will be invoked upon the `err.message`, thus converting all the HTML markup into text format.

You can also specify a base URI in the environment variable for rendering as `process.env.ERROR_HANDLER_BASE_URL`, e.g. `BERROR_HANDLER_BASE_URL=https://example.com` (omit trailing slash), and any HTML links such as `<a href="/foo/bar/baz">Click here</a>` will be converted to `[Click here][1]` with a `[1]` link appended of `https://example.com/foo/bar/baz`.


## License

[MIT](LICENSE) © Nick Baugh
Expand Down
41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,46 @@
"Nick Baugh <niftylettuce@gmail.com>"
],
"dependencies": {
"@hapi/boom": "^9.0.0",
"camelcase": "^5.3.1",
"capitalize": "^2.0.1",
"@hapi/boom": "^9.1.0",
"camelcase": "^6.0.0",
"capitalize": "^2.0.3",
"co": "^4.6.0",
"debug": "^4.1.1",
"html-to-text": "^5.1.1",
"humanize-string": "^2.1.0",
"lodash": "^4.17.15",
"statuses": "^1.5.0",
"statuses": "^2.0.0",
"toidentifier": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@koa/router": "^8.0.6",
"@koa/router": "^8.0.8",
"ava": "2.x",
"codecov": "^3.6.2",
"cross-env": "6.x",
"eslint": "^6.8.0",
"codecov": "^3.6.5",
"cross-env": "^7.0.2",
"eslint": "6.x",
"eslint-config-xo-lass": "^1.0.3",
"eslint-plugin-node": "^11.0.0",
"fixpack": "^2.3.1",
"husky": "3.x",
"eslint-plugin-node": "^11.1.0",
"fixpack": "^3.0.6",
"husky": "^4.2.5",
"koa": "^2.11.0",
"koa-404-handler": "^0.0.2",
"koa-basic-auth": "^4.0.0",
"koa-connect-flash": "^0.1.2",
"koa-convert": "^1.2.0",
"koa-generic-session": "^2.0.4",
"koa-redis": "^4.0.1",
"lint-staged": "^10.0.4",
"nyc": "^15.0.0",
"redis": "^2.8.0",
"remark-cli": "^7.0.1",
"remark-preset-github": "^0.0.16",
"lint-staged": "^10.2.2",
"nyc": "^15.0.1",
"redis": "^3.0.2",
"remark-cli": "^8.0.0",
"remark-preset-github": "^1.0.0",
"supertest": "^4.0.2",
"xo": "^0.25.3"
"xo": "0.25"
},
"engines": {
"node": ">=7.6.0"
Expand Down
28 changes: 23 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const path = require('path');
const Boom = require('@hapi/boom');
const Debug = require('debug');
const _ = require('lodash');
const capitalize = require('capitalize');
const camelCase = require('camelcase');
const capitalize = require('capitalize');
const co = require('co');
const htmlToText = require('html-to-text');
const humanize = require('humanize-string');
const statuses = require('statuses');
const toIdentifier = require('toidentifier');
Expand Down Expand Up @@ -97,7 +98,10 @@ async function errorHandler(err) {
err.statusCode = err.status;
this.statusCode = err.statusCode;
this.status = this.statusCode;
this.body = new Boom.Boom(err.message, {

const friendlyAPIMessage = makeAPIFriendly(this, err.message);

this.body = new Boom.Boom(friendlyAPIMessage, {
statusCode: err.status
}).output.payload;

Expand Down Expand Up @@ -212,6 +216,19 @@ async function errorHandler(err) {
this.res.end(this.body);
}

function makeAPIFriendly(ctx, message) {
if (!ctx.api) return message;
message = htmlToText.fromString(message, {
wordwrap: false,
linkHrefBaseUrl: process.env.ERROR_HANDLER_BASE_URL
? process.env.ERROR_HANDLER_BASE_URL
: '',
hideLinkHrefIfSameAsText: true,
ignoreImage: true
});
return message;
}

function parseValidationError(ctx, err) {
// translate messages
const translate = message =>
Expand Down Expand Up @@ -255,9 +272,10 @@ function parseValidationError(ctx, err) {
err.message = translate(_.values(err.errors)[0].message);
} else {
const errors = _.map(_.map(_.values(err.errors), 'message'), translate);
err.message = ctx.api
? errors.join(', ')
: `<ul class="text-left mb-0"><li>${errors.join('</li><li>')}</li></ul>`;
err.message = makeAPIFriendly(
ctx,
`<ul class="text-left mb-0"><li>${errors.join('</li><li>')}</li></ul>`
);
}

// this ensures the error shows up client-side
Expand Down
Loading

0 comments on commit 58a9571

Please sign in to comment.