A
A
Andrew2022-03-23 12:50:07
AJAX
Andrew, 2022-03-23 12:50:07

How to handle errors in PHP, specifically WebSocket connections?

The question for me is "systemic", that is, for the solution of which there is not enough knowledge and it is not known how to approach it.
From the menu via Ajax, the connection function via WebSockets is called. If the WS-server does not respond, then the message "probably an error connecting to the server" is returned from the function. I would like to display my message on the screen. If errors are disabled, then everything works, but I would like to implement error logging (if there is a reliable way, I will be grateful for the tip).
The option to parse the response from the "error: function (response)" branch does not suit me because of the long output, which I would like to write to a file (the user does not need it). Questions:
1 Is it possible to send along with the error (as an array element) and your own code/description of the error?
2 Is it possible somehow to make Ajax not respond to the error and handle the "success: function (response)" branch?
3 Is there another output parameter combining both branches "success + error" in which I would place the analysis of the answer?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2022-03-23
@larush

If possible, then I’m ready for this option - the text that I formed is placed in the error code, how can I get the resp value in JS ? I understand that the topic is PHP, but suddenly I'm just slowing down and everything is simpler than it actually is.
The answer was received via console.log(response['responseText']).
.
Warning : fsockopen(): Unable to connect to localhost : 7777 resp ":"...}

T
ThunderCat, 2022-03-23
@ThunderCat

Is it possible to send along with the error (as an array element) and your error code/description?
Of course, exceptions are usually used for this. Not everywhere, but in many cases they work fine. Another thing is that there are 2 nuances:
1) In case of a websocket server error, you will not receive any error from the server itself (it simply did not answer you), but in any case you will receive an error status that does not have to be given to the user, but based on the error number, give your message.
2)
but i would like to implement error logging
I hope you understand that it is impossible to do anything about this from the client side, but from the server side there is an error log, if these are not logic errors, which are not system errors, but rather incorrect behavior (such as "the field is not filled" or " Unacceptable symbols"). These can also be logged, but firstly - what's the point ?, and secondly, it's all the same to implement it with puff. And of course there will be no errors like "server not available".
2 Is it possible somehow to make Ajax not respond to the error and handle the ": function (response)" branch?
yes, more precisely, you just need to not process the result in success. See below.
3 Is there another output parameter combining both branches "success + error" in which I would place the analysis of the answer?
complete

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question