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

Return custom status code #1511

Open
jpcarpanezi opened this issue Aug 31, 2021 · 1 comment
Open

Return custom status code #1511

jpcarpanezi opened this issue Aug 31, 2021 · 1 comment
Labels
bug Identified as a potential bug help wanted Not actively being worked on. If you plan to contribute, please drop a note. high High priority medium effort Likely a few days of development effort

Comments

@jpcarpanezi
Copy link

Expected Behavior

  • Force return a custom status code in OcelotPipelineConfiguration for custom pre or override middleware

Actual Behavior

  • Can't return custom status code for custom middlewares. In my case, i want to return a 401 HTTP Status Code if my bearer token is in Redis blacklist, returning the follow stack trace:
requestId: 4000000b-0000-fb00-b63f-84710c7967bb, previousRequestId: no previous request id, message: Exception caught in global error handler, exception message: Object reference not set to an instance of an object., exception stack:    at Ocelot.Responder.HttpContextResponder.SetResponseOnHttpContext(HttpContext context, DownstreamResponse response)
         at Ocelot.Responder.Middleware.ResponderMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Ocelot.Errors.Middleware.ExceptionHandlerMiddleware.Invoke(HttpContext httpContext) RequestId: 4000000b-0000-fb00-b63f-84710c7967bb, exception: System.NullReferenceException: Object reference not set to an instance of an object.
         at Ocelot.Responder.HttpContextResponder.SetResponseOnHttpContext(HttpContext context, DownstreamResponse response)
         at Ocelot.Responder.Middleware.ResponderMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Ocelot.Errors.Middleware.ExceptionHandlerMiddleware.Invoke(HttpContext httpContext)
      System.NullReferenceException: Object reference not set to an instance of an object.
         at Ocelot.Responder.HttpContextResponder.SetResponseOnHttpContext(HttpContext context, DownstreamResponse response)
         at Ocelot.Responder.Middleware.ResponderMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)
         at Ocelot.Errors.Middleware.ExceptionHandlerMiddleware.Invoke(HttpContext httpContext)

Steps to Reproduce the Problem

  1. Add OcelotPipelineConfiguration in Startup.cs Configure method
var configuration = new OcelotPipelineConfiguration {
	PreAuthorizationMiddleware = async (ctx, next) => {
		string token = ctx.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");

		if (!string.IsNullOrEmpty(token)) {
			string blacklist = await cache.GetStringAsync(token);

			if (string.IsNullOrEmpty(blacklist)) {
				ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
				await ctx.Response.WriteAsync("Não");
				return;
			}
		}

                await next.Invoke();
	}
};
  1. Add pipeline to UseOcelot() method
    await app.UseOcelot(configuration);

Specifications

  • Version: 17.0.0
  • Platform: Windows, .NET 5
  • Subsystem: IIS Express
@raman-m
Copy link
Member

raman-m commented Jan 21, 2024

Hi @jpcarpanezi !
Welcome to Ocelot world! 🐯

We know about this problem of original status code overriding by Error Mapper... 😞
In docs we have this page: Error Status Codes

This is very ugly and redundant feature which behaves strange, and it breaks a lot of downstream services.
We have to recover original status code and keep it in HTTP response.
But custom Ocelot status can be moved to a new header.

@raman-m raman-m added bug Identified as a potential bug help wanted Not actively being worked on. If you plan to contribute, please drop a note. medium effort Likely a few days of development effort high High priority labels Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identified as a potential bug help wanted Not actively being worked on. If you plan to contribute, please drop a note. high High priority medium effort Likely a few days of development effort
Projects
None yet
Development

No branches or pull requests

2 participants