Skip to content

Commit

Permalink
feat: create new handleFetch with custom error handling (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
meppsilon authored Jan 5, 2022
1 parent cf6565d commit 1d25f43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import {
replayHistory,
generateHistoryEntry,
getStxProcessingTime,
handleFetch,
} from './utils';
import { CHAIN_IDS } from './constants';

const { handleFetch, safelyExecute } = util;
const { safelyExecute } = util;

// TODO: JSDoc all methods
// TODO: Remove all comments (* ! ?)
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,20 @@ export const getStxProcessingTime = (
}
return Math.round((Date.now() - smartTransactionSubmittedtime) / 1000);
};

export async function handleFetch(request: string, options?: RequestInit) {
const response = await fetch(request, options);
const json = await response.json();
if (!response.ok) {
const { error: type, error_details: message } = json;
console.log(`response`, response);
throw new Error(
`Fetch error:${JSON.stringify({
status: response.status,
type,
message,
})}`,
);
}
return json;
}

0 comments on commit 1d25f43

Please sign in to comment.