Skip to content

request help: Support customizing the body when response exits #3565

Description

@nanamikon

Issue description

Relate to #3528

I think we can add a feature to customize the body when response exits. For example, different format of json, even html
or xml , can be use to display the exit message

Refer to apisix/core/response.lua, resp_exit will be called when response exits, I think it is a global handle for response exiting, we can't use plugin extension here。 What about redirecting to a new uri to do the actual work ?

Add method

function _M.register_exit_handle_uri(uri){
       handle_uri = uri
}

Modify apisix/core/response.lua
Add exec to redirect to error handle

    if handle_uri then
        ngx.exec(handle_uri, {code=xxx, body=yyy});
    else
        if idx > 0 then
          ngx_print(concat_tab(t, "", 1, idx))
        end
    
        if code then
            return ngx_exit(code)
        end   
    end

Add new config to change this behavior

apisix:
      exit_handle_uri:"/error_handle"
     

New uri for error handle,added by http_server_configuration_snippet

    location = /error_handle {
      content_by_lua '
        local args = ngx.req.get_uri_args()
        ngx.print(args.body)
        return ngx.exit(args.code)
      ';
    }

Some discussion about kong

1 Kong error response
Kong/kong#3192

2 Upstream error response
Kong/kong#972

Environment

  • apisix version (cmd: apisix version):2.2
  • OS (cmd: uname -a):ubuntu 16
  • OpenResty / Nginx version (cmd: nginx -V or openresty -V): 1.19
  • etcd version, if have (cmd: run curl http://127.0.0.1:9090/v1/server_info to get the info from server-info API):3.4.17
  • apisix-dashboard version, if have:

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions