Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Jan 29, 2019
1 parent 6f65fe1 commit edbba8f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 100 deletions.
86 changes: 0 additions & 86 deletions README.fr.md

This file was deleted.

2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#### If you are looking for a command-line tool, check out [linky-cli](https://github.com/bokub/linky-cli)

#### > [Lire en français](https://github.com/bokub/linky/blob/master/README.fr.md) <


## Install

Expand Down
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const axios = require('axios');
const dayjs = require('dayjs');

const enedisNotice = ' - Check the Enedis website if the error persists';
const get = (p, o) => p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o);
const parseDate = dt => dayjs(`${dt.substr(6, 4)}-${dt.substr(3, 2)}-${dt.substr(0, 2)}`);

Expand All @@ -20,15 +21,15 @@ async function login(email, password) {
});
} catch (err) {
if (!err.response || err.response.status !== 302) {
throw new Error('Unexpected login response (2): ' + err.message);
throw new Error('Unexpected login response (2): ' + err.message + enedisNotice);
}

const cookies = err.response.headers['set-cookie']
.filter(el => el.indexOf('Domain=.enedis.fr') > -1)
.filter(el => el.indexOf('Expires=Thu, 01-Jan-1970 00:00:10 GMT') === -1);

if (!cookies || cookies.length === 0) {
throw new Error('Unexpected login response (3)');
throw new Error('Unexpected login response (3)' + enedisNotice);
}
const authCookies = cookies.filter(h => h.indexOf('iPlanetDirectoryPro=') === 0);
if (authCookies.length === 0) {
Expand All @@ -37,7 +38,7 @@ async function login(email, password) {
return new LinkySession(cookies);
}

throw new Error('Unexpected login response (1)');
throw new Error('Unexpected login response (1)' + enedisNotice);
}

class LinkySession {
Expand Down Expand Up @@ -100,7 +101,7 @@ class LinkySession {
});
} catch (err) {
if (!err.response || err.response.status !== 302 || this.calledOnce) {
throw new Error('Unexpected export response (2): ' + err.message);
throw new Error('Unexpected export response (2): ' + err.message + enedisNotice);
}
const newCookies = err.response.headers['set-cookie'];
this.cookies = this.cookies.concat(newCookies);
Expand All @@ -117,7 +118,7 @@ class LinkySession {
});
return resp.data;
} catch (err) {
throw new Error('Unexpected export response (3): ' + err.message);
throw new Error('Unexpected export response (3): ' + err.message + enedisNotice);
}
}

Expand All @@ -129,7 +130,7 @@ class LinkySession {
throw new Error('Please log in manually and accept the new terms of service');
}

throw new Error('Unexpected export response (1): ' + resp.data);
throw new Error('Unexpected export response (1): ' + resp.data + enedisNotice);
}

getCookie() {
Expand All @@ -151,7 +152,7 @@ class LinkySession {

const values = get(['graphe', 'data'], input);
if (!Array.isArray(values)) {
throw new Error(`Unexpected data: ${JSON.stringify(input)}`);
throw new Error(`Unexpected data: ${JSON.stringify(input)}` + enedisNotice);
}

const start = parseDate(get(['graphe', 'periode', 'dateDebut'], input));
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@bokub/linky",
"description": "Easily retrieve your Linky power consumption",
"version": "0.1.0",
"version": "0.2.0",
"author": "bokub",
"bugs": "https://github.com/bokub/linky/issues",
"dependencies": {
"axios": "^0.18.0",
"dayjs": "^1.7.7"
"dayjs": "^1.8.0"
},
"devDependencies": {
"xo": "^0.23.0"
Expand Down

0 comments on commit edbba8f

Please sign in to comment.