Skip to content

Commit 1d25f43

Browse files
authored
feat: create new handleFetch with custom error handling (#35)
1 parent cf6565d commit 1d25f43

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/SmartTransactionsController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ import {
2727
replayHistory,
2828
generateHistoryEntry,
2929
getStxProcessingTime,
30+
handleFetch,
3031
} from './utils';
3132
import { CHAIN_IDS } from './constants';
3233

33-
const { handleFetch, safelyExecute } = util;
34+
const { safelyExecute } = util;
3435

3536
// TODO: JSDoc all methods
3637
// TODO: Remove all comments (* ! ?)

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,20 @@ export const getStxProcessingTime = (
149149
}
150150
return Math.round((Date.now() - smartTransactionSubmittedtime) / 1000);
151151
};
152+
153+
export async function handleFetch(request: string, options?: RequestInit) {
154+
const response = await fetch(request, options);
155+
const json = await response.json();
156+
if (!response.ok) {
157+
const { error: type, error_details: message } = json;
158+
console.log(`response`, response);
159+
throw new Error(
160+
`Fetch error:${JSON.stringify({
161+
status: response.status,
162+
type,
163+
message,
164+
})}`,
165+
);
166+
}
167+
return json;
168+
}

0 commit comments

Comments
 (0)