Open Source API Gateway

500 Internal Server Error: Choose Expression Evaluation Failed

Membrane API Gateway returns 500 Internal Server Error when a test expression of a case inside a choose plugin fails at runtime. The cause is the expression in the gateway configuration, or a message the expression does not expect.

The Error Response

The response contains the status 500 Internal Server Error, the problem type https://membrane-api.io/problems/internal and a link to this page. In development mode, it also contains the failing expression and details such as a body excerpt or the failed type conversion. The detail currently says in if plugin, although the error comes from choose. The type internal means that the gateway failed while processing the exchange.

How choose Behaves After the Error

The choose plugin evaluates the test of each case in order and runs the flow of the first case that matches, or the otherwise branch:

api:
port: 2000
flow:
- choose:
- case:
test: exchange.request.uri.matches('^/api/v1/.')
flow:
- destination:
url: http://v1-backend.internal.local
- otherwise:
- destination:
url: http://default-backend.internal.local
api:
  port: 2000
  flow:
    - choose:
        - case:
            test: exchange.request.uri.matches('^/api/v1/.')
            flow:
              - destination:
                  url: http://v1-backend.internal.local
        - otherwise:
            - destination:
                url: http://default-backend.internal.local

Currently, a failing expression sets this error as the response, but does not abort the exchange. No case is taken, and the otherwise branch runs if one exists. Otherwise the flow continues. In the request flow, the response of the backend can therefore replace this error, so the client does not always see it. Do not rely on this behavior.

Common Causes

A SpEL Expression Fails

The expression calls a method or function that does not exist, a value cannot be converted to the needed type, or a Membrane built-in function fails.

A JSONPath Expression Fails

The path is invalid, or the JSON body cannot be evaluated with it.

An XPath Expression Fails

The expression uses a namespace prefix that is not declared, or the XML body has extra characters before the XML declaration or after the root element.

Syntax errors in SpEL and JSONPath expressions are reported at startup. This error is about evaluating an expression against a concrete message.

How to Fix It

  1. Read the failing expression and the details in the response in development mode, or in the gateway log.
  2. Correct the test expression, or make it robust against messages it does not expect, for example by checking the content type first in an earlier case.
  3. For XPath, declare the namespace prefixes you use.
  4. Client side: make sure the body matches the content type the API expects.

Hide Details in Production

By default, Membrane runs in development mode and adds details to error responses that help you find the problem, including the link to this page. These details may reveal information about your setup. In production, turn them off:

configuration:
production: true
configuration:
  production: true

Add the configuration document to your apis.yaml and separate it from the APIs with ---.

In production mode, the title of this error is replaced by Internal Server Error, and the response only says Internal details are hidden. See server log (key: ...). Search the gateway log for that key to find the full error message.

Related Documentation

Configuration reference:

Tutorials with runnable examples:

← All Membrane error messages

Still stuck? See the troubleshooting guide or ask on GitHub Issues. For commercial support, contact info@predic8.de.