JSON response in case of any error

So I need Cloudflare to produce an application/json response when my GraphqlServer APIs go down or If there is a timeout error.

Something like:
{“success”: false, “cause” : "some error message”}

Currently, I am getting an error like:

    <html>
<head><title>524 Origin Time-out</title></head>
<body bgcolor="white">
<center><h1>524 Origin Time-out</h1></center>
<hr><center>cloudflare-nginx</center>
</body>
</html>

Just so that I can show these messages directly, as I am using a Nodejs package to handle request/response and it expect JSON response.

Could you please help me with this?