Skip to content

Sending custom error message to a client

By default, if backend server returns 4xx error in response to /connect method or /publish (or another direct invoke) method, the reason phrase to send to a client is taken from response header. For example, if response to /connect is

HTTP/1.1 403 Forbidden
Date: Tue, 01 Jun 2021 04:49:40 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 44
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

then the following Websocket message will be sent to the client

{
  "message": "fail",
  "data": [{
    "status": "FAILED",
    "info": "Forbidden",
    "apiMethod": "ConnectionStatusEvent"
  }]
}

Since build 5.2.968 it is possible to send a custom reason phrase to the client in this case. To do this, the backend should return the following JSON object

HTTP/1.1 403 Forbidden
Date: Tue, 01 Jun 2021 04:49:40 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 44
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

{
  "status":403,
  "reason":"Wrong credentials"
}

The following message will be sent to the client

{
  "message": "fail",
  "data": [{
    "status": "FAILED",
    "info": "Wrong credentials",
    "apiMethod": "ConnectionStatusEvent"
  }]
}

The JSON object should contain status and reason fields only. If one of the fields is absent, or some other fields are added to the response object, the reason phrase to send to the client will be taken from backend server response header, as done by default.