message: Pare down module, introduce pcio http error class
diff: "use strict";
  -const logger = require("commonlog-bunyan").logger;
  -const jsonApiResponseFilter = require("./filter");
  +const { logger } = require("commonlog-bunyan");
  +const filter = require("./filter");
  +const {
  + PostcodesioHttpError,
  +} = require("../app/lib/errors.js");
  +
  +const genericError = new PostcodesioHttpError();
  /**
  * Returns JSON response on behalf of routes that return `response.jsonApiResponse`
  @@ -9,16 +14,12 @@ const jsonApiResponseFilter = require("./filter");
  * CORS is enabled at this layer.
  * If JSONP is detected, a 200 response is returned regardless of success.
  */
  -function jsonApiResponseRenderer (request, response, next) {
  +const renderer = (request, response, next) => {
  const jsonResponse = response.jsonApiResponse;
  if (!jsonResponse) return next();
  -
  - if (request.query.callback) {
  - response.status(200).jsonp(jsonResponse);
  - } else {
  - response.status(jsonResponse.status).json(jsonResponse);
  - }
  -}
  + if (request.query.callback) return response.status(200).jsonp(jsonResponse);
  + return response.status(jsonResponse.status).json(jsonResponse);
  +};
  const invalidJsonErrorMessage = [
  "Invalid JSON submitted.",
  @@ -26,16 +27,18 @@ const invalidJsonErrorMessage = [
  "Also ensure that Content-Type is set to application/json"
  ].join(" ");
  -const serverErrorMessage = [
  - "500 Server Error.",
  - "For an urgent fix email support@ideal-postcodes.co.uk.",
  - "Alternatively submit an issue at https://github.com/ideal-postcodes/postcodes.io/issues"
  -].join(" ");
  +/**
  + * Applies an instance of PostcodesioHttpError to a response
  + * @param {Express.Response} res - Express response object
  + * @param {PostcodesioHttpError} err - PostcodesioHttpError instance
  + * @returns {undefined}
  + */
  +const applyError = (res, err) => res.status(err.status).json(err.toJSON());
  /**
  * Handles Requests that have resulted in an error. Invoked by next(someError)
  */
  -function errorRenderer (error, request, response, next) {/*jshint unused: false */
  +const errorRenderer = (error, request, response, next) => {/*jshint unused: false */
  logger.error({error: error.message});
  if (process.env.NODE_ENV !== "test") console.log(error.stack);
  @@ -50,25 +53,22 @@ function errorRenderer (error, request, response, next) {/*jshint unused: false
  }
  // Return 500 for all other errors
  - return response.status(500).json({
  - status: 500,
  - error: serverErrorMessage
  - });
  -}
  + return applyError(response, genericError);
  +};
  /**
  * Handles requests that have fallen through middleware stack by returning a 404
  */
  -function notFoundRenderer (request, response) {
  +const notFoundRenderer = (request, response) => {
  response.status(404).json({
  status: 404,
  error: "Resource not found"
  });
  -}
  +};
  module.exports = app => {
  - app.use(jsonApiResponseFilter);
  - app.use(jsonApiResponseRenderer);
  + app.use(filter);
  + app.use(renderer);
  app.use(errorRenderer);
  app.use(notFoundRenderer);
  };
  