coupergateway / couper

Couper is a lightweight API gateway designed to support developers in building and operating API-driven Web projects
https://couper.io
MIT License
85 stars 15 forks source link

api level error handler must affect endpoint's buffer options #706

Closed johakoch closed 1 year ago

johakoch commented 1 year ago

Consider the following config:

server {
  api {
    endpoint "/anything" {
      request "r" {
        url = "https://blackhole.webpagetest.org/"
        backend {
          timeout = "1s"
        }
      }

      proxy {
        backend {
          origin = "https://httpbin.org"
        }
      }
    }

    error_handler "backend_timeout" {
      response {
        status = 418
        json_body = {
          resp_json_body_headers = backend_responses.default.json_body.headers
        }
      }
    }
  }
}

The error handler's BufferOpts are BufferResponse (because of the reference to json_body), whereas the endpoint's are BufferNone. So the proxy response's body is not buffered, and resp_json_body_headers is null.

In order to make this config work, api level error handlers must affect api level endpoints' BufferOpts.


Note: Test_StoreInvalidBackendResponse with its configuration testdata/integration/error_handler/06_couper.hcl:

server {
  api {
    endpoint "/anything" {
      proxy {
        backend {
          origin = "${env.COUPER_TEST_BACKEND_ADDR}"

          openapi {
            file = "02_schema.yaml"
          }
        }
      }
    }

    error_handler "backend_openapi_validation" {
      response {
        status = 418
          json_body = {
            req_path = backend_requests.default.path
            resp_status = backend_responses.default.status
            resp_json_body_query = backend_responses.default.json_body.Query
            resp_ct = backend_responses.default.headers.content-type
          }
      }
    }
  }
}

only works because both BufferOpts have BufferResponse set, the endpoint's because of the openapi block, the error handler's because of a reference to json_body.