mirror of
https://github.com/BradNut/weddingsite
synced 2025-09-08 17:40:36 +00:00
25 lines
624 B
JavaScript
25 lines
624 B
JavaScript
|
|
export default async function fetcher(...args) {
|
||
|
|
try {
|
||
|
|
const response = await fetch(...args);
|
||
|
|
|
||
|
|
// if the server replies, there's always some data in json
|
||
|
|
// if there's a network error, it will throw at the previous line
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
if (response.ok) {
|
||
|
|
return data;
|
||
|
|
}
|
||
|
|
|
||
|
|
// console.log(JSON.stringify(data));
|
||
|
|
const error = new Error(response?.statusText || data?.message);
|
||
|
|
error.response = response;
|
||
|
|
error.data = data;
|
||
|
|
throw error;
|
||
|
|
} catch (error) {
|
||
|
|
if (!error.data) {
|
||
|
|
error.data = { message: error.message };
|
||
|
|
}
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|