helper.js hinzugefügt
diese Datei hat eine methode handleResponse() welche die fetchanfrage übernimmt und dessen response handlet.
This commit is contained in:
1 parent
c6bc6cf8f9
commit
4622e91f26
1 file changed
+22
@@ -0,0 +1,22 @@
|
||||
import { showError } from "./error.js";
|
||||
|
||||
|
||||
export async function handleResponse(promise, errorMsg) {
|
||||
const response = await promise;
|
||||
|
||||
//weil 204 kein body ist un damit unparsbar, geben wir vorab true zurück
|
||||
if (response.status == 204) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
showError(json.msg);
|
||||
console.error(errorMsg);
|
||||
return false;
|
||||
}
|
||||
|
||||
return json;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user