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

session.respond_error with custom headers #381

Open
lebanggit opened this issue Sep 13, 2024 · 4 comments
Open

session.respond_error with custom headers #381

lebanggit opened this issue Sep 13, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@lebanggit
Copy link

What is the problem your feature solves, or the need it fulfills?

response_filter was not call after throw error by session.respond_error

Additional context

Please add solution for this

@drcaramelsyrup drcaramelsyrup added the enhancement New feature or request label Sep 13, 2024
@drcaramelsyrup
Copy link
Contributor

One option is to reuse your response filter logic to modify the response header from HttpSession::generate_error and call write_response_header directly, which is all that respond_error is doing.

In the phase chart you'll notice this is currently by design, a bit different from how nginx might work. The response filter will get called for upstream or cached responses, but writing the response header directly short circuits the state machine.

@lebanggit
Copy link
Author

lebanggit commented Sep 14, 2024

@drcaramelsyrup Thanks!!!

I did it

use pingora_core::protocols::http::ServerSession as HttpSession;
async fn show_error(session: &mut Session, error: u16) -> Result<()> {
    let mut resp = HttpSession::generate_error(error);

    resp.insert_header("X-Error-Type", "Internal");
    resp.insert_header("X-Error-From", "Lib-xxxxxx");
    session.write_response_header(Box::new(resp), true)
        .await
        .unwrap_or_else(|e| {
            session.downstream_session.set_keepalive(None);
            error!("failed to send error response to downstream: {e}");
        });
    Ok(())
}

It's good if it comes with a message.

@drcaramelsyrup
Copy link
Contributor

Great!

It's good if it comes with a message.

Do you mean there's some enhancement we could make to our docs? It would be helpful to know what that would look like.

@lebanggit
Copy link
Author

Great!

It's good if it comes with a message.

Do you mean there's some enhancement we could make to our docs? It would be helpful to know what that would look like.

Just add body content (ex: html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants