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

Adding ES error cases to createError #64

Merged
merged 1 commit into from
Feb 25, 2022

Conversation

RC-Lee
Copy link
Contributor

@RC-Lee RC-Lee commented Feb 19, 2022

Follow up of #63

Gave createError its own module so we could add cases for ElasticSearch errors.
Updated the tests to reflect the changes as well.

To test: pnpm test

For this change, the ES error will be created into an http-error indirectly first, before being sent into createError again as a nested error with additional props.
Doing so this way, we get to keep the ES error statusCode, it won't be overwritten.
However, I have no way of changing the description name of the final error to whatever ES might have named the specific status code to. The names are based on the status code that createError receives.
For example, a 404 status code will always be created as a NotFoundError, as shown below

    const elasticError = new errors.ResponseError({
      body: { error: 'testing ElasticSearch Error' },
      statusCode: 404,
    });

    const testESError = createError(503, elasticError);
    console.log(testESError);

The log would be something like

 NotFoundError: ElasticSearch Error:ResponseError
        at ... {
      body: { error: 'testing ElasticSearch Error' }
    }

So, it might be up for discussion if we want to keep the ES error status codes, or if we have it so they can be overwritten.

const _createError = require('http-errors');
const { errors } = require('@elastic/elasticsearch');

module.exports.createError = (...args) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module.exports = (...args) => {

for (let i = 0; i < args.length; i++) {
let arg = args[i];
let type = typeof arg;
if (type === 'object' && arg instanceof errors.ResponseError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments please to indicate what each path is doing in this if.

src/index.js Outdated
@@ -4,7 +4,7 @@ const { createRouter } = require('./app');
module.exports.Satellite = require('./satellite');
module.exports.logger = require('./logger');
module.exports.hash = require('./hash');
module.exports.createError = require('http-errors');
module.exports.createError = require('./createError').createError;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module.exports.createError = require('./create-error');

NOTE: always name files in lower-case, since not all filesystems are case sensitive.

@@ -0,0 +1,22 @@
const _createError = require('http-errors');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this file to src/create-error.js

@RC-Lee
Copy link
Contributor Author

RC-Lee commented Feb 24, 2022

Changed the file name, added comments, and changed the module.exports line.
I've also added a new test to show cases where a status code is not included as the first argument.
http-errors will set the status to 500 in such cases.

  test('errors created without a status code should return a status of 500', () => {
    expect(createError('testing').status).toBe(500);
  });

@RC-Lee RC-Lee requested a review from humphd February 24, 2022 23:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants