-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onError
is not called when a network error occurs in query
and batch
methods
#141
Comments
onError
is not called when a network error occurs using the query
and batch
methodsonError
is not called when a network error occurs in query
and batch
methods
Yeah, it is thrown here: Everything above 400 will call this method. Do you have an example where this does not work? |
It's not called for network errors, so if you block the API call in your browser's console, fetch will throw a public async query(query?: OdataQuery) {
try {
this.config.onStart(this);
const response: Response[] = await this.getFetch(query); // <------ A TypeError is thrown when the API cannot be reached
const json = await Promise.all(
response.map(
async (res) => {
if (res.status >= 400) {
this.config.onError(this, res);
throw res;
} else if (res.ok && res.json) {
try {
this.config.onFinish(this, res);
const data = await res.json();
return data[this.config.fragment] || data;
} catch (ex) {
return res;
}
} else {
return await res.text();
}
},
),
);
return json.length > 1 ? json : json[0];
} catch (ex) {
throw ex; // <---------- The TypeError is rethrown without calling onError
} finally {
this.requests = [];
}
} To have a similar behaviour to the public async query(query?: OdataQuery) {
try {
this.config.onStart(this);
const response: Response[] = await this.getFetch(query);
const json = await Promise.all(
response.map(
async (res) => {
if (res.status >= 400) {
throw res;
} else if (res.ok && res.json) {
try {
this.config.onFinish(this, res);
const data = await res.json();
return data[this.config.fragment] || data;
} catch (ex) {
return res;
}
} else {
return await res.text();
}
},
),
);
return json.length > 1 ? json : json[0];
} catch (ex) {
this.config.onError(this, ex);
throw ex;
} finally {
this.requests = [];
}
} |
hi, yeah might be a good improvement. I would accept a PR for this. |
When a network error occurs, a
TypeError
is thrown and thefetch
method passes it to theonError
handler, butquery
doesn't. In order to be able to handle all possible errors, I guess the behaviour should be aligned between the two methods.Here how the catch looks like in the three methods:
Are you open to a PR that fixes this?
Thanks!
The text was updated successfully, but these errors were encountered: