input-output-hk / mithril

Stake-based threshold multi-signatures protocol
https://mithril.network
Apache License 2.0
130 stars 41 forks source link

fix(aggregator): properly handle `404` errors in http server #2130

Closed Alenar closed 1 week ago

Alenar commented 1 week ago

Content

This PR fix the handling of 404 errors in the aggregator http server.

In case of a 404 the custom rejection middleware immediatly emit a rejection (this custom rejection was added to set the appropriate status code for API Version Mistmatch errors). This means that no other middleware would be applied nor our custom headers.

Pre-submit checklist

Comments

This problem cascade to the browser javascript fetch api, the missing headers make the api immediately throw an error thus making impossible to handle the 404 in a idiomatic way.

In this example the status code can't be checked in the then block since a exception is raised (note: the exception doesn't contains the status code).

fetch(aggregatorEndpoint)
  .then((response) => {
    if (response.status === 404) {
      // do something with it
    } else {
      return response.status === 200 ? response.json() : {};
    }
  })
  .catch((error) => {
    console.error("Fetch status error:", error);
  });

This problems made more cumbersome the fallback to Epoch Setting mechanism used in PR #2128, we had to handle it in the catch block without the ability to check that it was indeed a 404 or not.

github-actions[bot] commented 1 week ago

Test Results

    4 files  ±0     51 suites  ±0   11m 50s ⏱️ +17s 1 446 tests +1  1 446 ✅ +1  0 💤 ±0  0 ❌ ±0  1 657 runs  +1  1 657 ✅ +1  0 💤 ±0  0 ❌ ±0 

Results for commit 5ca6033a. ± Comparison against base commit 43b267a9.

:recycle: This comment has been updated with latest results.