question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow plugins to set response status code

See original GitHub issue

When there is an issue that means an entire request has failed, which would normally result in an a 200 status code, it would be useful to be able to return something other than a 200.

Currently, if we want to change the response via a plugin (within willSendResponse for example), the following will change it to a 200, providing the original code was an error code: requestContext.response.http.status = 200; However, if the response is a 200, the opposite will not change it to an error code: requestContext.response.http.status = 500;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
qsonacommented, Jun 10, 2021

👍 I’m a user of apollo-server and I really want this feature. We basically monitor the rate of http status code and use it as service level indicator. It’s a difficult problem that how to treat partial errors (some fields are correctly calculated and some are not), but as a first step we’d like to treat partial errors as 5xx error.

Seems related to: https://github.com/apollographql/apollo-server/issues/473

1reaction
burtontannercommented, Dec 17, 2021

@awinograd Thanks! That worked for me. Just to add some more detail for someone else trying to do this. Create a plugin like so

  import {ApolloServerPlugin} from "apollo-server-plugin-base/src/index";
  
  const statusCodePlugin:ApolloServerPlugin  = {
    async requestDidStart(requestContext) {
      return {
        async willSendResponse(requestContext) {
          const errors = (requestContext?.response?.errors || []) as any[];
          for(let error of errors){
            if(error?.code === 'unauthorized'){
              requestContext.response.http.status = 401;
            }
            if(error?.code === 'access'){
              requestContext.response.http.status = 403;
            }
          }
        }
      }
    },
  };
  
  export default statusCodePlugin;
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
Using Spring @ResponseStatus to Set HTTP Status Code
In this short tutorial, we will see the most straightforward way: using the @ResponseStatus annotation.
Read more >
Status codes in HTTP - W3C
Status codes. The values of the numeric status code to HTTP requests are as follows. The data sections of messages Error, Forward and...
Read more >
hapi — How to Set Response Status Code - Future Studio
This guide is the first one geared towards responses and precisely shows you how to set the response status code that follows the...
Read more >
Set Response Code - Akamai TechDocs
With Status Code set to 200, enable 206 Override to allow the 206 OK status to take precedence. Note: Using the 206 override...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found