Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve location and property_id error response to return 400 status instead of 500 server error #4884

Merged

Conversation

andres-torres-marroquin
Copy link
Contributor

@andres-torres-marroquin andres-torres-marroquin commented May 13, 2024

Closes PROD-2035

Description Of Changes

Improved validation error messages for fides.js.

Code Changes

  • Improved error message when geolocation param is provided but is invalid, e.g. `Invalid request: geolocation "foo" is invalid, must supply an ISO 3166-2 location code
  • 4xx-class errors are returned for other types of invalid requests for property_id param, and for IS_OVERLAY_ENABLED, IS_PREFETCH_ENABLED and FIDES_STRING environment variables.

Steps to Confirm

  • Try using a geolocation param with wrong location text in fides.js, so it checks the format.
  • Try using property_id param without geolocation param.

Pre-Merge Checklist

  • All CI Pipelines Succeeded
  • Issue Requirements are Met
  • Update CHANGELOG.md

Copy link

vercel bot commented May 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
fides-plus-nightly ⬜️ Ignored (Inspect) Visit Preview May 15, 2024 7:08pm

Copy link

cypress bot commented May 13, 2024

Passing run #7739 ↗︎

0 4 0 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge 75c9df0 into 0f9acea...
Project: fides Commit: 5ec7cdf70e ℹ️
Status: Passed Duration: 00:37 💡
Started: May 15, 2024 12:43 AM Ended: May 15, 2024 12:43 AM

Review all test suite changes for PR #4884 ↗︎

@galvana galvana self-requested a review May 14, 2024 03:51
Copy link
Contributor

@galvana galvana left a comment

Choose a reason for hiding this comment

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

This worked as expected! I just made some small recommendations to clean up the tests a bit and a way to handle errors in the lookupGeolcation call

);
} catch (error) {
if (error instanceof Error) {
console.error(error);
Copy link
Contributor

Choose a reason for hiding this comment

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

Add this line before the console.error to disable the lint error

Suggested change
console.error(error);
// eslint-disable-next-line no-console
console.error(error);

console.error(error);
res
.status(400) // 400 Bad Request. Malformed request.
.send(error.message);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can do this instead of checking if error is an instance of Error and then you could fallback to still return an error message instead of just the empty return

res
  .status(400) // 400 Bad Request. Malformed request.
  .send(
    error instanceof Error ? error.message : "An unknown error occurred."
  );

Comment on lines 265 to 275
try {
await lookupGeolocation(req);
} catch (error) {
if (error instanceof Error) {
expect(error.message).toMatch(
"Provided location (US-NewYork) query parameter is not in ISO 3166 format."
);
} else {
expect(typeof error).toBe(Error);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify these checks using Jest's toThrow syntax

await expect(lookupGeolocation(req)).rejects.toThrow(
  "Provided location (US-NewYork) query parameter is not in ISO 3166 format."
);

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 think for this one it throws an Error object and not a string. So I think this may not work, will check it now and comment back.

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 was wrong, this works perfectly, I like it, thanks!

@galvana galvana merged commit 1d7c694 into main May 15, 2024
12 checks passed
@galvana galvana deleted the PROD-2035-improve-geolocation-and-property-id-error-response branch May 15, 2024 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants