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

[Telescope#2755] Adding more tests for createError #63

Merged
merged 1 commit into from
Feb 19, 2022

Conversation

RC-Lee
Copy link
Contributor

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

Requested from #2755

What I did
Added more tests to createError

What was found?
It's ElasticSearch's fault. They have errors that are structured differently than regular http-errors causing the failure.

How to test
pnpm test

@RC-Lee RC-Lee self-assigned this Feb 17, 2022
@RC-Lee
Copy link
Contributor Author

RC-Lee commented Feb 18, 2022

Added more tests. Shows how we can indirectly createError from an ES Error

@AmasiaNalbandian
Copy link
Contributor

holy smokes Roxanne!!! I'm running it now. :)

});

try {
createError(503, elasticError);
Copy link
Contributor

Choose a reason for hiding this comment

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

were bringing back a 503 because elastic returns 404 not available correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just replicating what's being done in the query.js that's causing the error

// query.js
router.get('/', validateQuery, async (req, res, next) => {
  try {
    const { text, filter, page, perPage } = req.query;
    res.send(await search(text, filter, page, perPage));
  } catch (error) {
    console.log({ error }, 'query error');
    next(createError(503, error));   // <-- crash is here
  }
});

If you look below in the indirect test, there are different ways we can send in the "correct" status code.
The thing is though, ES has different status code names for their errors than the regular http errors. Even if we send the ES status code in for createError, it'll just name it depending on the http error and not the ES error.

Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

Question: in a follow-up, since we expose createError and Elastic from Satellite, should we do something internally to have this work as expected by our users? That is, should we add to createError() an extra case to deal with extracting the right bits from an Elastic error automatically?

@RC-Lee
Copy link
Contributor Author

RC-Lee commented Feb 19, 2022

Yes, I think we probably could.

I think right now in Satellite there's no module for createError, it's just exporting createError from http-errors
Link to line

module.exports.createError = require('http-errors');

We could probably create its own module to modify it the way we want to.

Based on the documentation, all of ES errors should be called ResponseError

Errors: there is no longer a custom error class for every HTTP status code (such as BadRequest or NotFound). 
There is instead a single ResponseError. Every error class has been renamed, and now each is suffixed with Error at the end. 

I have been looking at the list of http-errors. It doesn't seem like there's one called ResponseError, so we might be able to filter errors based off of its name, and then extract the ES error.
Or, since we're already using ElasticSearch in Satellite anyways, filter it off of a ResponseError instance

The question perhaps is just how much detail, or which object, we want to send in to createError

@RC-Lee
Copy link
Contributor Author

RC-Lee commented Feb 19, 2022

I'm not sure if I have it down right. But since createError is an imported function, does that mean it is a const and if we want to export our own versions, maybe we'd have to rename our exported function?

@RC-Lee RC-Lee merged commit 127aba4 into DevelopingSpace:master Feb 19, 2022
@humphd
Copy link
Contributor

humphd commented Feb 19, 2022

const _createError = require('http-error');

module.exports.createError = (...) => {.
  // Do your stuff to fix up the err if it's from Elasticsearch...
  return _createError(...);
};

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.

3 participants