Skip to content

Commit

Permalink
fix: adds specific error classes like parse error for handling them
Browse files Browse the repository at this point in the history
closes #27
  • Loading branch information
Ephigenia committed Sep 27, 2020
1 parent e09328f commit 8895226
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions source/cli-stock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const storesData = require('./lib/stores');

let pkg = require('./../package.json');
let IOWS2 = require('./lib/iows2.js');
const errors = require('./lib/iows2Errors');

function optionalSplitOptionCSV(val) {
const seperator = ',';
Expand Down Expand Up @@ -118,6 +119,9 @@ Examples:
return iows.getStoreProductAvailability(store.buCode, productId)
// softly continue the promise chain when there’s just a 404 (not found)
.catch(err => {
if (err instanceof errors.IOWS2ParseError) {
return { stock: 0, probability: 'PARSE_ERROR', createdAt: new Date() };
}
// when product could not be found return an empty availability
if (err.response && err.response.status === 404 && promises.length > 1) {
return { stock: 0, probability: 'NOT_FOUND', createdAt: new Date() };
Expand Down
2 changes: 2 additions & 0 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');

const IOWS2 = require('./lib/iows2');
const stores = require('./lib/stores');
const errors = require('./lib/iows2Errors');

module.exports = {
/**
Expand All @@ -27,4 +28,5 @@ module.exports = {
return iows.getStoreProductAvailability(buCode, productId)
},
stores,
errors,
};
22 changes: 16 additions & 6 deletions source/lib/iows2.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const stores = require('./stores');
* Estimated date when the item gets restocked. Can be empty
*/

const errors = require('./iows2Errors');

/**
* @class IOWS2
*/
Expand Down Expand Up @@ -84,7 +86,7 @@ class IOWS2 {
.then(response => {
debug('RECEIVED', response.status);
if (!response.ok) {
const err = new Error(`Unexpected http status code ${response.status}`);
const err = new errors.IOWS2ResponseError(`Unexpected http status code ${response.status}`);
// add context variables to error object for provide more information
err.request = { url, params: params };
err.response = response;
Expand Down Expand Up @@ -113,7 +115,7 @@ class IOWS2 {
// RestockDateTime may contain an estimated date when the product gets
// restocked. It also can be missing in the response
let restockDate = null;
if (availability.RetailItemAvailability.RestockDateTime) {
if (availability.RetailItemAvailability && availability.RetailItemAvailability.RestockDateTime) {
restockDate = new Date(availability.RetailItemAvailability.RestockDateTime.$);
}

Expand Down Expand Up @@ -179,10 +181,18 @@ class IOWS2 {
throw err;
})
.then(data => {
data = IOWS2.parseAvailabilityFromResponseData(data);
data.buCode = buCode;
data.productId = productId;
return data;
let parsed = {
buCode,
productId,
};
try {
parsed = Object.assign(parsed, IOWS2.parseAvailabilityFromResponseData(data));
} catch (err) {
let error = new errors.IOWS2ParseError('Unable to parse valid looking response: ' + err.message);
error.data = data;
throw error;
}
return parsed;
});
}
}
Expand Down
11 changes: 11 additions & 0 deletions source/lib/iows2Errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

class IOWS2Error {}
class IOWS2ParseError extends IOWS2Error{}
class IOWS2ResponseError extends IOWS2Error{}

module.exports = {
IOWS2Error,
IOWS2ParseError,
IOWS2ResponseError,
}

0 comments on commit 8895226

Please sign in to comment.