Skip to content

Commit

Permalink
Merge pull request #30 from NYPL/lc_error_logging
Browse files Browse the repository at this point in the history
Better logging messages MLN-238
  • Loading branch information
LucasCioffi authored Sep 5, 2018
2 parents 090e11d + c9c3363 commit 2c68876
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ $ npm run deploy-package-production
*To get your AWS Lambda service credentials, please visit [AWS Lambda's website](https://aws.amazon.com/lambda/).*

## Development Change Log
### v0.3.1
#### Update
- update error handling and log messages

### v0.3.0
#### Add
- add a v0.2 create-patron API endpoint
Expand Down
17 changes: 10 additions & 7 deletions api/controllers/v0.1/createPatron.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,14 @@ function createPatron(req, res) {
modelStreamPatron.transformSimplePatronRequest(
req.body, modeledResponse // eslint-disable-line comma-dangle
)
.then(streamPatron => streamPublish.streamPublish(
process.env.PATRON_SCHEMA_NAME_V01,
process.env.PATRON_STREAM_NAME_V01,
streamPatron // eslint-disable-line comma-dangle
))
.then((streamPatron) => { // eslint-disable-line arrow-body-style
// `return` is necessary below, to wait for streamPublish to complete
return streamPublish.streamPublish(
process.env.PATRON_SCHEMA_NAME_V01,
process.env.PATRON_STREAM_NAME_V01,
streamPatron // eslint-disable-line comma-dangle
);
})
.then(() => {
renderResponse(req, res, 201, modeledResponse);
logger.debug('Published to stream successfully!', { routeTag: ROUTE_TAG });
Expand Down Expand Up @@ -180,8 +183,8 @@ function createPatron(req, res) {
modelResponse.errorResponseData(responseObject) // eslint-disable-line comma-dangle
);
} else {
renderResponse(req, res, 500, modelResponse.errorResponseData(
collectErrorResponseData(null, '', '', '', '') // eslint-disable-line comma-dangle
renderResponse(req, res, response.response.status, modelResponse.errorResponseData(
collectErrorResponseData(response.response.status, '', '', '', `${response.message} from NYPL Simplified Card Creator.`) // eslint-disable-line comma-dangle
));
}
});
Expand Down
3 changes: 1 addition & 2 deletions api/controllers/v0.2/createPatron.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const streamPublish = require('./../../helpers/streamPublish');
const logger = require('../../helpers/Logger');
const encode = require('../../helpers/encode');
const customErrors = require('../../helpers/errors');
const util = require('util');

const ROUTE_TAG = 'CREATE_PATRON_0.2';
let ilsClientKey;
Expand Down Expand Up @@ -205,7 +204,7 @@ function callAxiosToCreatePatron(req, res) {
const errorResponseData = modelResponse.errorResponseData(
collectErrorResponseData(axiosError.response.status, '', axiosError.response.data, '', '') // eslint-disable-line comma-dangle
);
renderResponse(req, res, 500, errorResponseData);
renderResponse(req, res, axiosError.response.status, errorResponseData);
} catch (error) {
const errorResponseData = modelResponse.errorResponseData(
collectErrorResponseData(500, '', `Error related to ${process.env.ILS_CREATE_PATRON_URL} or publishing to the NewPatron stream.`, '', '') // eslint-disable-line comma-dangle
Expand Down
2 changes: 1 addition & 1 deletion api/models/v0.2/modelResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function modelErrorResponseData(obj) {
detail: {
title: obj.title || '',
debug: (obj.debug_message) ? parseJSON(obj.debug_message) : {},
}
},
};
}

Expand Down
66 changes: 50 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ app.use(bodyParser.urlencoded({

// Below are the middlewares for response body

/**
* v1Error(err, req)
* Format the response for errors on v0.1 routes
*
* @param {error object} err
* @param {HTTP request} req
*/
function v1Error(err, req) { // eslint-disable-line no-unused-vars
return {
data: {
status_code_from_card_creator: null,
status: err.status,
type: 'invalid-request',
message: `Request body: ${err.body}`,
detail: 'The patron creator did not forward the request to Card Creator.',
},
count: 0,
};
}

/**
* v2Error(err, req)
* Format the response for errors on v0.2 routes
*
* @param {error object} err
* @param {HTTP request} req
*/
function v2Error(err, req) { // eslint-disable-line no-unused-vars
return {
status_code_from_card_ils: null,
status: err.status,
type: 'invalid-request',
message: `Request body: ${err.body}`,
detail: 'The patron creator did not forward the request to the ILS.',
};
}

/**
* errorHandler(err, req, res, next)
* Rendering the error response if the request to this service fails.
Expand All @@ -53,21 +90,18 @@ function errorHandler(err, req, res, next) { // eslint-disable-line no-unused-va
console.error(
`status_code: ${err.status}, ` +
'type: "invalid-request", ' +
`message: "error request with ${err.body}"` // eslint-disable-line comma-dangle
`message: "Request body: ${err.body}"` // eslint-disable-line comma-dangle
);

res
.status(err.status)
.json({
data: {
status_code_from_card_creator: null,
status_code_from_card_ils: null,
type: 'invalid-request',
message: `Error with this request body: ${err.body}`,
detail: {},
},
count: 0,
});
if (req.url.includes('v0.1')) {
res
.status(err.status)
.json(v1Error(err, req));
} else if (req.url.includes('v0.2')) {
res
.status(err.status)
.json(v2Error(err, req));
}
}

// Error handling
Expand Down Expand Up @@ -95,9 +129,9 @@ const config = {
if (!process.env.AWS_LAMBDA_FUNCTION_NAME) {
const port = process.env.PORT || 3001;

app.listen(port, function () {
console.info('Server started on port ' + port)
})
app.listen(port, () => {
console.info(`Server started on port ${port}`); // eslint-disable-line no-console
});
}

module.exports = app;

0 comments on commit 2c68876

Please sign in to comment.