Skip to content
This repository has been archived by the owner on Mar 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #25 from lemonde/format-error
Browse files Browse the repository at this point in the history
chore(error): retire cheerio et extrait uniquement le message du html
  • Loading branch information
mariusbrn authored Apr 7, 2020
2 parents 9e8db99 + 54be04d commit e1fc17f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
17 changes: 4 additions & 13 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@ function convertParam(val) {
}

function parseHtmlError(message) {
const $ = cheerio.load(message);
if ($('html').length <= 0) return new Error(message)
if (typeof message !== 'string') return new Error(message)

const match = $("h1")
.text()
.match(/([\d]{3})/);
const status = parseInt(match, 10);
const match = message.match(/(?<=h1>)(.+?)(?=<\/h1>)/)
if (!match) return new Error(message)

return new Error({
status,
message: $("b:contains('message')")
.siblings("u")
.text(),
stack: $("pre").text()
});
return new Error(match[0]);
}

exports.convertParam = convertParam;
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-oss-client",
"version": "2.1.3",
"version": "2.1.4",
"description": "Node.js client for Open Search Server.",
"main": "index.js",
"scripts": {
Expand All @@ -22,7 +22,6 @@
"url": "https://github.com/lemonde/node-oss-client/issues"
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"lodash": "~3.7.0",
"request": "~2.55.0",
"underscore.string": "~3.0.3"
Expand Down
21 changes: 21 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,25 @@ describe('Util', () => {
expect(util.convertParam(object)).to.equal(object);
});
});

describe('#parseHtmlError', () => {
it('should return erro if not a string', () => {
const err = util.parseHtmlError(500)
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('500');
});

it('should extract message from html', () => {
const htmlError = '<html><body><h1>My error</h1></body></html>'
const err = util.parseHtmlError(htmlError)
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('My error');
});

it('should return raw message if not html', () => {
const err = util.parseHtmlError('My error')
expect(err instanceof Error).to.be.true;
expect(err.message).to.be.equal('My error');
});
});
});

0 comments on commit e1fc17f

Please sign in to comment.