The definition of the 502 Bad Gateway status code is:
The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.
Is it an appropriate status code to respond with if the error was caused by the database? Despite the fact that the definition of the 500 Internal Server Error status code doesn't state that the error has to be internal, I don't like using it when the error is external.
Can the web server be considered to be a gateway or a proxy, since it is performing tasks other than communicating with the database, such as making calls to other APIs and services?
No, I don't think an HTTP 502 Bad Gateway error is appropriate when a database error occurs.
HTTP errors say something about the HTTP protocol. This specific error indicates a server is trying to relay the HTTP request, but the upstream server did not respond correctly.
Your web application communicating with a database server is outside the realm of HTTP and any errors should be wrapped in the generic HTTP 500 Internal server error response code.
There is no reference for the Database error status code. You can use 500 Internal Server Error as a response.
Related
I am using React to create an application and using a PHP server for the API. I realized when there is a PHP error i am unable to see what the error is. The only thing i get back is
'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Is there anyway where I can see what is the error message from the php side?
Actually, the current error message is pretty comprehensive. You need to configure CORS correctly by adding appropriate headers.
Cross-Origin Request Headers(CORS) with PHP headers
You are getting the CORS issue because your PHP server is running on a different port compared to your react server (react dev server, running on port 3000).
There are two action items to be done in this case:
You may whitelist localhost:3000 on your PHP server (you can check this answer https://stackoverflow.com/a/33090515/13142033).
Use a logger service in your PHP code. For example monolog (https://github.com/Seldaek/monolog). So, you may use a try-catch or generic exception handlers in your PHP code, and whenever there is an error use the logger to write to a file on the disk.
Actually, when you fix the CORS issue, you will see PHP error logs in your react API responses, but having a logger service is a good practice.
I have React app in front-end (client), calling API provided by Flask back-end (server) via axios package.
Both client and server are running locally. Client: localhost:3000. Server: localhost:5000
The problem is: after many requests, the server can not receive request from client.
Here is the picture of the received requests, which is captured in backend:
As you can see, after some success request, the React app stuck with pending request:
The lastest request: 127.0.0.1 - - [11/Jun/2020 09:34:39] "GET /posts HTTP/1.1" is error 500, but in network tab of chrome, the request is still pending, so i dont know if the server received that request or not. Nothing shown in console log of chrome, no error printed in backend terminal windows (i have some lines of code to print error in backend), just the error 500
What am i doing wrong? If this question is still confused, please comment below and i can update more info about it. Thank you!
The fact that your server is reporting a 500 error, but the request on the client is still pending makes me think that something is wrong on the server side.
To verify this, you can try manually calling raise Exception() in one of your endpoints. Then instead of seeing the error resolve through the react app, you can try calling your endpoint with curl, or a client like Postman. If you are able to see a 500 error there, then the error is likely in your React app. If that request does not resolve, then the error is probably in the server.
Is an error response is being sent to the client? The way to do this in flask is using error handlers.
Thank you for helping me.
i debugged my server and found that 2 request call from react app were served by 1 cursor
(i used 1 connector.cursor for the whole connection => 2 request arrives in one moment => the cursor does not know how to serve the result)
=> Solution: change 2 request to 1 new request to new API, in new API, return the result which is containing both result of that 2 request
My current project is a restful API that connects to a database to retrieve data.
What should be done if the route handlers can't access the database? In such a case I would implement a middleware that sends back a global response that indicates that the API is temporarily not available.
Which status code should be returned? Internal Server Error (500) or Service Unavailable (503)?
Is it efficient if the middleware checks with each request the database connection?
Well normal people dont understand 500 or 503 . so its better to catch these exceptions in catch block and then set some appropriate message .
Also for all your database related question explore database connection use connection pooling lib such as HakariCp
When working with the realtime API I sometimes get this error:
Failed to load resource: the server responded with a status of 400 (Unknown SID) https://drive.google.com/otservice/bind?id=13bt-KPBrU6WRdWSqLBl2cni366tuZqT…&RID=rpc&SID=7808B6FE50C4A180&CI=0&AID=15&TYPE=xmlhttp&zx=64nyubda57et&t=1
What does it mean?
This error is a known issue with the Realtime API and we are working to track down the cause. In general, the API will automatically retry the request and you can ignore the error.
If the processing of an email fails (eg. API timeout) should I return a 500 (404 ?) or throw an Exception.
Whilst it's undocumented I'm assuming that AppEngine with behave similarly to task queues and retry the http call to deliver the email. Is this a reasonable assumption?
Since the mail system uses webhooks and interacts with App Engine at the HTTP level, your only option is to return an error status. If you throw an exception, your framework will convert this into a 500 response. That's a legitimate way to return a 500 - so how you do this is up to you.