luraproject / lura

Ultra performant API Gateway with middlewares. A project hosted at The Linux Foundation
https://luraproject.org
Other
6.35k stars 563 forks source link

In `Sequential Proxy` how to use the backend response code instead of gateway response 500 #250

Closed Michael2008S closed 5 years ago

Michael2008S commented 5 years ago

In Sequential Proxy How to use the backend response code and content instead of 500 -Error #01: Invalid status code? For the first request's response.

The gateway log :

[GIN] 2019/04/25 - 17:23:23 | 500 |  261.535456ms |    113.88.96.84 | GET      /gcrpc/coupon.CouponRecipient/ViewByCode?code=8cf56df3-44e4-4c57-bb2e-4994c5ec467
Error #01: Invalid status code

The backend response is below:

> POST /gcrpc/coupon.CouponRecipient/ViewByCode HTTP/1.1
> Host: dt.xxx.com
> User-Agent: curl/7.54.0
> accept: application/json
> Authorization: Bearer eyJhbGciOiJIUz....
> Content-Type: application/json
> Content-Length: 48
>
* upload completely sent off: 48 out of 48 bytes
< HTTP/1.1 400 Bad Request
< Server: nginx/1.10.3 (Ubuntu)
< Date: Thu, 25 Apr 2019 09:26:19 GMT
< Content-Type: application/json
< Content-Length: 82
< Connection: keep-alive
< X-Krakend: Version undefined
< X-Krakend-Completed: false
<
* Connection #0 to host 127.0.0.1 left intact
{"code":"invalid_argument","msg":"优惠券码不存在pg: no rows in result set"}⏎

This is my config:

  {
      "backend": [
        {
          "disable_host_sanitize": true, 
          "encoding": "JSON", 
          "extra_config": {
            "github.com/devopsfaith/krakend-martian": {
              "body.FromQuerystring": {
                "keys_to_extract": ["id"], 
                "method": "POST", 
                "template": "{\"code\":\"{{index .code 0}}\"}"
              }
            }
          }, 
          "host": ["micro-rpc-coupon"], 
          "method": "POST", 
          "sd": "etcd", 
          "url_pattern": "/gcrpc/coupon.CouponRecipient/ViewByCode"
        }, 
        {
          "disable_host_sanitize": true, 
          "encoding": "JSON", 
          "extra_config": {
            "github.com/devopsfaith/krakend-martian": {
              "body.FromQuerystring": {
                "keys_to_extract": ["id"], 
                "method": "POST", 
                "template": "{\"id\":\"{{index .id 0}}\"}"
              }
            }
          }, 
          "group": "shops", 
          "host": ["micro-rpc-retail_shops"], 
          "method": "POST", 
          "sd": "etcd", 
          "url_pattern": "/gcrpc/retail.shops.Shops/View?id={resp0_shop_id}"
        }, 
        {
          "disable_host_sanitize": true, 
          "encoding": "JSON", 
          "extra_config": {
            "github.com/devopsfaith/krakend-martian": {
              "body.FromQuerystring": {
                "keys_to_extract": ["id"], 
                "method": "POST", 
                "template": "{\"id\":\"{{index .id 0}}\"}"
              }
            }
          }, 
          "group": "recipient_user", 
          "host": ["micro-rpc-user"], 
          "method": "POST", 
          "sd": "etcd", 
          "url_pattern": "/gcrpc/user.User_aggregation/View?id={resp0_recipient_uid}"
        }, 
        {
          "disable_host_sanitize": true, 
          "encoding": "JSON", 
          "extra_config": {
            "github.com/devopsfaith/krakend-martian": {
              "body.FromQuerystring": {
                "keys_to_extract": ["id"], 
                "method": "POST", 
                "template": "{\"id\":\"{{index .id 0}}\"}"
              }
            }
          }, 
          "group": "shop_distributor", 
          "host": ["micro-rpc-user"], 
          "method": "POST", 
          "sd": "etcd", 
          "url_pattern": "/gcrpc/user.User_aggregation/View?id={resp0_shop_distributor_uid}"
        }
      ], 
      "endpoint": "/gcrpc/coupon.CouponRecipient/ViewByCode", 
      "extra_config": {
        "github.com/devopsfaith/krakend/proxy": {
          "sequential": true
        }
      }, 
      "headers_to_pass": ["*"], 
      "method": "GET", 
      "querystring_params": ["code"]
    }, 
kpacha commented 5 years ago

hi @Michael2008S

whenever you want to merge responses from different backends (either concurrently or sequentially), the status codes should be 'normalized'.

you can enable the return_error_details on your backends and you'll get some detail of the errors in your response body but it is not possible to forward status codes.

if you enable the return_error_details, maybe your next request won't get the required data to fulfill the URL to hit

cheers

Michael2008S commented 5 years ago

Ok,Thanks.

Frontrider commented 4 years ago

Kinda necroing this thread, but @kpacha's response should be added to the docs. It's obvious when you think about it, but not by default.

github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.