Firestore Connection Dropped Error in React Unit Test - reactjs

I'm using Firestore as my backend for my React app. I'm implementing unit testing, but it throws me an error as such:
#firebase/firestore: Firestore (9.14.0): Connection GRPC stream error. Code: 14 Message: 14 UNAVAILABLE: Connection dropped
After that, it logs me another error:
#firebase/firestore: Firestore (9.14.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: 14 UNAVAILABLE: Connection dropped
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend
My guess is that I'm unable to call to the database when unit testing.
Is there a way I can work around this error? Or do I have to make a mock for it? If yes, is there a reference I can refer to regarding this?

Related

How do i make calls to the Firestore database after a user is signed in?

I'm trying to setup Firebase with React. I can make calls to the database (Firestore) without errors, however, anytime I try to make a call to the database after logging in I get two error messages. The errors only occur after a user has signed in. The ruleset for the database is:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Error messages:
1) Firestore (7.15.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unknown]: Fetching auth token failed: getToken aborted due to token change.
2) FirebaseError: Failed to get document because the client is offline.
Picture of the error messages
Github Repo
It sounds like you're describing an issue that was fixed in 7.15.1, so I suggest updating. According to the release notes:
Fixed an issue that prevented the client from connecting to the backend immediately after a user signed in. See GitHub issue #2923.

React WebSocket on server, Error during WebSocket handshake

I have made an application that uses a WebSocket. I want to run this application on a PLESK Server, but I get this error message 'WebSocket connection to 'wss://sub.domain.com/' failed: Error during WebSocket handshake: Unexpected response code: 404' when I visit the website.
After reading a few blogs, I came to the conclusion you have to adjust something in the NGINX settings, but this also appears to have no effect, or I have to do it wrong, that is also possible. (Another blog)
At the moment our API is installed on a subdomain (with the NodeJS add-on in plesk) and it also works. As soon as I start up the files locally I can connect to the WebSocket and the API so it must be up to the PLESK Server I think. At this moment only the NGINX 'Proxy mode' is off and the 'Additional nginx directives' empty.
Perhaps one of you is familiar with this problem?

Is 502 an appropriate status code for a database error?

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.

Restful API without database connection

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

GAE App gets socket errors when communicating with BigQuery

Our GAE python application communicates with BigQuery using the Google Api Client for Python (currently we use version 1.3.1) with the GAE-specific authentication helpers. Very often we get a socket error while communicating with BigQuery.
More specifically, we build a python Google API client as follows
1. bq_scope = 'https://www.googleapis.com/auth/bigquery'
2. credentials = AppAssertionCredentials(scope=bq_scope)
3. http = credentials.authorize(httplib2.Http())
4. bq_service = build('bigquery', 'v2', http=http)
We then interact with the BQ service and get the following error
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/gae_override/httplib.py", line 536, in getresponse
'An error occured while connecting to the server: %s' % e)
error: An error occured while connecting to the server: Unable to fetch URL: [api url...]
The error raised is of type google.appengine.api.remote_socket._remote_socket_error.error, not an exception that wraps the error.
Initially we thought that it might be timeout-related, so we also tried setting a timeout altering line 3 in the above snippet to
3. http = credentials.authorize(httplib2.Http(timeout=60))
However, according to the log output of client library the API call takes less than 1 second to crash and explicitly setting the timeout did not change the system behavior.
Note that the error occurs in various API calls, not just a single one, and usually this happens on very light operations, for example we often see the error while polling BQ for a job status and rarely on data fetching. When we re-run the operation, the system works.
Any idea why this might happen and -perhaps- a best-practice to handle it?
All HTTP(s) requests will be routed through the urlfetch service.
Beneath that, the Google Api Client for Python uses httplib2 to make HTTP(s) requests and under the covers this library uses socket.
Since the error is coming from socket you might try to set the timeout there.
import socket
timeout = 30
socket.setdefaulttimeout(timeout)
If we continue up the stack httplib2 will use the timeout parameter from the socket level timeout.
http://httplib2.readthedocs.io/en/latest/libhttplib2.html
Moving further up the stack you can set the timeout and retries for BigQuery.
try:
timeout = 30000
num_retries = 5
query_request = bigquery_service.jobs()
query_data = {
'query': (query_var),
'timeoutMs': timeout,
}
And finally you can set the timeout for urlfetch.
from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(30)
If you believe it's timeout related you might want to test each library / level to make sure the timeout is being passed correctly. You can also use a basic timer to see the results.
start_query = time.time()
query_response = query_request.query(
projectId='<project_name>',
body=query_data).execute(num_retries=num_retries)
end_query = time.time()
logging.info(end_query - start_query)
There are dozens of questions about timeout and deadline exceeded for GAE and BigQuery on this site so I wouldn't be surprised if you're hitting something weird.
Good luck!

Resources