Is it possible for Axios to differentiate between the following:
A request which fails because the client has no network connection at
the point when the request was made - (ERR_CONNECTION_REFUSED).
A request which fails because network connectivity is lost after the
request was made but before the response was received - (ERR_INTERNET_DISCONNECTED).
It seems that in both cases, all we get back is a rather unhelpful "Network Error" message...
The Axios documentation (https://github.com/axios/axios#handling-errors) suggests that in the latter case, error.request should be populated, but I have been unable to replicate any scenario where error.request exists. In both of the above scenarios, I get an error object looking like this:
{
config: { ... },
response: undefined,
message: "Network Error",
stack: "Error: Network Error at createError (http://localhost:3000/static/js/bundle.js:1636:15) at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:1170:14)"
}
I don't know exacly if you can get ERR_CONNECTION_REFUSED but ERR_INTERNET_DISCONNECTED you can get use native web Network Information API,take a look on snippet bellow:
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
console.log(connection);
When connection.type: "none" mean that you are ERR_INTERNET_DISCONNECTED, maybe you can explore this solution.
you can read complete documention here, cya
Related
I am trying to run an old react app, it compiles successfully on Terminal but on the browser it does not run and gives me this message "Request failed with status code 423"
I am a beginner, I have searched and found this:
The 423 Locked status code means the source or destination resource of a method is locked. This response SHOULD contain an appropriate precondition or postcondition code, such as ‘lock-token-submitted’ or ‘no-conflicting-lock’.
But I don't know what should I do. Any idea or solution?
first, you have to find the API call that makes the error, then handle/catch the error to prevent app crashing, e.g:
catch method:
axios(...).catch(console.error);
async/await:
async function apiCall() => {
try {
const {
data
} = axios(...);
} catch (error) {
console.error(error)
}
}
and about the HTTP request error, you should ask about that from the API maintainer/developer.
I'm using Firebase-Firestore on Javascript (web) with a Progressive web app. I ran into this error:
INTERNAL ASSERTION FAILED: Got result for empty write pipeline
Because Firebase runs asynchronously with XHR requests, it was difficult to determine the exact source of the error - it seemed like any onSnapshot, set or update was throwing this error for me.
And after that first error came a flurry of other errors:
INTERNAL ASSERTION FAILED: AsyncQueue is already failed: Error: FIRESTORE (5.3.0) INTERNAL ASSERTION FAILED: Got result for empty write pipeline
I thought my operation was pretty normal - just using the API set(), update() , onSnapshot() functions when it happened.
It's not a mission critical error - the code runs fine, but I'm hit with a couple thousand errors when I open debug, so it's prohibitive in that regard.
For my PWA I was using a cache-first, web-reupdate model which returns cachedResponse but also fetch()es the response and caches the fetched response.
Anyone have any insights?
It was the PWA! Using the PWA, I was catching all GET requests, including Firebase's own GET's. Filtering to ensure CORS requests don't return from cache fixed the problem.
To solve this, I added this code to my PWA:
self.addEventListener("fetch", event => {
if (event.request.method == "GET") {
event.respondWith(
(async function() {
const cachedResponse = await cache.match(event.request, {
ignoreSearch: true
});
// Returned the cached response if we have one, otherwise return the network response.
if (cachedResponse && event.request.type!="cors") {
//AVOID CORS FOR THINGS LIKE FIREBASE
updateCache(event);
return cachedResponse;
} else return await updateCache(event);
})()
);
} else {
event.respondWith(fetch(event.request));
}
});
If you're new to the PWA space, want to get a jump start to ANY PWA project, or want to just 'share notes', the repo with the full comprehensive PWA file is here: https://github.com/acenturyandabit/genUI/blob/master/Javascript/pwa.js
I've personally put a lot of time into this so I hope it helps :)
I'm using opentok client JS api OpenTok.js 2.14.6 753045900, to set up a video conferencing app. Intermittently, it throws errors into chrome console:
WebSocket connection to 'wss://mantis002-ams.tokbox.com/rumorwebsocketsv2?socketId=12796b11-4174-431e-980f-52c29c32aa0d&attempt=b58d3a4d-7721-4e93-b39b-76224b00864a' failed: WebSocket is closed before the connection is established.
WebSocket connection to 'wss://mantis002-ams.tokbox.com/rumorwebsocketsv2?socketId=12796b11-4174-431e-980f-52c29c32aa0d&attempt=0d49b33e-33b9-4eec-a6d4-80ea6d7908ec' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
OT:RumorSocket:1 Error code unavailable.
OT.Raptor.Socket error: e {code: 4020, message: "Error code unavailable."}
OT_SOCKET_CLOSE_FALLBACK_CODE: Error code unavailable.
OT.exception :: title: undefined (4020) msg: Error code unavailable.
s {code: 4020, message: "Error code unavailable.", name: "OT_SOCKET_CLOSE_FALLBACK_CODE", stack: "Error: Error code unavailable.↵ at https://stat…tatic.opentok.com/v2/js/opentok.min.js:29:247034)"}
An error occurred in an event handler TypeError: Cannot read property 'id' of
null
at index.js:455
at P (RaptorSocket.js:75)
at r (RaptorSocket.js:265)
at d.triggerCallback (Dispatcher.js:32)
at n.<anonymous> (SessionDispatcher.js:179)
at n.emit (events.js:84)
at d.trigger (eventing.js:321)
at d.emit (eventing.js:330)
at RaptorSocket.js:279
How to correctly handle this situation, because seems like reconnection doesn't work inside OpenTok.js?
And when I tried to recreate session I received
OT.exception :: title: Unexpected Server Response (2001) msg: Unexpected server response. Try this operation again later.
{ code: 2001, message: "Unexpected server response. Try this operation again later.", name: "OT_UNEXPECTED_SERVER_RESPONSE", stack: "Error: Unexpected server response. Try this operat…/static.opentok.com/v2/js/opentok.min.js:23:8742)"}
mixin.js:23 OT:RumorSocket:49 The connection is being terminated because the endpoint has indicated that reconnections are not available. (CLOSE_UNSUPPORTED)
TokBox Developer Evangelist here.
Could you please email support#tokbox.com with the sessionId along with the logs? The support team will be able to help you discover the underlying issue. You can also use the Session Inspector tool for a high-level summary that may help you pinpoint the connection errors.
I have the following code in say abcd.js:
$http({url: 'some url' , method: 'POST', data: , headers: {}
}).then(function(response)) {
.............
}, function error(response) {
..............
})
In the case of error the values of response.status = -1,
response.statusText ="". Basically no useful info. However in the Chrome Debugger console output I see:
POST: Some URL/analysis.php net::ERR_NETWORK_IO_SUSPENDED
The chrome debugger extracts the real error from the analysis.php network packet and displays it.
Q1: Why is that the status and statusText dont have useful info?
Q2: Is it possible to get programmatically the network error? In the above failure example I would get it as ERR_NETWORK_IO_SUSPENDED.
Q3: Programmatically is there any other way to get the network error when $http() fails?
Q1: Why is that the status and statusText dont have useful info?
because you cannot reach the server, you won't get useful status code from the server.
Q2: Is it possible to get programmatically the network error?
you can set timeout to $http to catch network problems manually.
Q3: Programmatically is there any other way to get the network error when $http() fails?
take advantage of error callback of $http? (I don't know well about this)
After much Googling I found out that when ever I get the status = -1 that means the server was not reached at all because of some unknown reasons. All the unknown reasons are bundled under one error string ERR_NETWORK_IO_SUSPENDED. I do get a -1 and now I have simply hard coded the error string as ERR_NETWORK_IO_SUSPENDED. For other error code such as 400, 404 the status does get the number bad statuText has the correct string. So this confirmed $http() call is fine. With that said we can close this case. Thanks to all who helped to arrive at this conclusion.
Is it possible to programmatically check if a WebSocket connection failed with a 403 response? For instance, with the following server code:
package main
import (
"errors"
"io"
"log"
"net/http"
"golang.org/x/net/websocket"
)
func main() {
handler := websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws)
})
handshake := func(conf *websocket.Config, req *http.Request) error {
if req.URL.Path == "/sekret" {
return nil
}
return errors.New("Oops!")
}
server := &websocket.Server{
Handshake: handshake,
Handler: handler,
}
log.Fatal(http.ListenAndServe(":8080", server))
}
And the following sample JS connection that triggers a 403 response:
var ws = new WebSocket("ws://localhost:8080/something");
ws.onerror = console.log;
The error response is an Event with type "error". On the other hand, the following JS code triggers a net::ERR_CONNECTION_REFUSED (at least on Chrome):
var ws = new WebSocket("ws://localhost:8081/something");
ws.onerror = console.log;
The error event object looks almost exactly the same. Is there some way to distinguish error types in WebSocket connections from the client side? (I'm specifically looking for 403 responses, so I can alert the user to take special action.)
Apparently it's deliberate:
My understanding is that for security reasons, WebSocket error statuses are fairly restricted to limit the ability of malicious JS to probe an internal network.