-
Notifications
You must be signed in to change notification settings - Fork 3
Feat/mkt 11337 error hosting support #139
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9976df2
feat:error handling and new region endpoint support
Amitkanswal 386894b
fix:refactor error handling logic
Amitkanswal 0fbf787
fix:removed response type
Amitkanswal 72c3dcc
ref:updated error logic
Amitkanswal 1523b08
fix:added support for network error
Amitkanswal f3ab178
fix:removed node-fetch and updated node version dependencies
Amitkanswal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| import { AxiosRequestConfig, AxiosResponse, } from 'axios' | ||
| export type RequestConfig = AxiosRequestConfig | ||
| export type ProxyResponse = AxiosResponse | ||
| import { AxiosRequestConfig, AxiosResponse } from "axios"; | ||
| export type RequestConfig = AxiosRequestConfig; | ||
| export type ProxyResponse = AxiosResponse; | ||
|
|
||
| export type ContentstackEndpoints = { | ||
| APP: string; | ||
| CMA: string; | ||
| DEVELOPER_HUB: string; | ||
| [key:string]:string; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,95 @@ | ||
| import PostRobot from 'post-robot'; | ||
| import { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
| import { onError, fetchToAxiosConfig } from './utils'; | ||
| import PostRobot from "post-robot"; | ||
| import { Response } from "node-fetch"; | ||
| import { | ||
| AxiosError, | ||
| AxiosHeaders, | ||
| AxiosRequestConfig, | ||
| AxiosResponse, | ||
| } from "axios"; | ||
|
|
||
| import { fetchToAxiosConfig } from "./utils"; | ||
|
|
||
| /** | ||
| * Dispatches a request using PostRobot. | ||
| * @param postRobot - The PostRobot instance. | ||
| * @returns A function that takes AxiosRequestConfig and returns a promise. | ||
| */ | ||
| export const dispatchAdapter = (postRobot: typeof PostRobot) => (config: AxiosRequestConfig)=> { | ||
| return postRobot | ||
| .sendToParent("apiAdapter", config ) | ||
| .then(({ data }) => ({ ...data, config })) | ||
| .catch(onError); | ||
| }; | ||
| export const dispatchAdapter = | ||
| (postRobot: typeof PostRobot) => (config: AxiosRequestConfig) => { | ||
| return new Promise((resolve, reject) => { | ||
| postRobot | ||
| .sendToParent("apiAdapter", config) | ||
| .then((event: unknown) => { | ||
| const { data: response } = event as { data: AxiosResponse }; | ||
|
|
||
| if (response.status >= 400) { | ||
| return reject({ ...response, config }); | ||
| } | ||
| resolve({ | ||
| data: response.data, | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| headers: response.headers, | ||
| config: config, | ||
| }); | ||
| }) | ||
| .catch(() => { | ||
| return reject( | ||
| new AxiosError( | ||
| "Something went wrong with the request", | ||
| "ERR_INTERNAL_SERVER", | ||
| { | ||
| ...config, | ||
| headers: config.headers as AxiosHeaders, | ||
| }, | ||
| null, | ||
| undefined | ||
| ) | ||
| ); | ||
| }); | ||
| }); | ||
| }; | ||
| /** | ||
| GitHub Copilot | ||
| To handle errors generically and robustly, we can refactor the code to ensure that all errors are properly processed and converted into a standardized format. Here's how you can fix and improve the error handling in the provided code: | ||
|
|
||
| Refactored Code | ||
|
|
||
| * Dispatches an API request using axios and PostRobot. | ||
| * @param url - The URL of the API endpoint. | ||
| * @param options - Optional request options. | ||
| * @returns A promise that resolves to a partial Response object. | ||
| */ | ||
| export const dispatchApiRequest = async (url: string, options?: RequestInit): Promise<Response> => { | ||
| try { | ||
| const config = fetchToAxiosConfig(url, options); | ||
| const responseData = await dispatchAdapter(PostRobot)(config) as AxiosResponse; | ||
| return new Response(responseData.data,{ | ||
| status: responseData.status, | ||
| statusText: responseData.statusText, | ||
| headers: new Headers(responseData.config.headers || {}), | ||
| }); | ||
| export const dispatchApiRequest = async ( | ||
| url: string, | ||
| options?: RequestInit | ||
| ): Promise<Response> => { | ||
| try { | ||
| const config = fetchToAxiosConfig(url, options); | ||
| const response = (await dispatchAdapter(PostRobot)( | ||
| config | ||
| )) as AxiosResponse; | ||
|
|
||
| } catch (error: any) { | ||
| if (error.response) { | ||
| const fetchResponse = new Response(error.response.data, { | ||
| status: error.response.status, | ||
| statusText: error.response.statusText, | ||
| headers: new Headers(error.response.headers) | ||
| }); | ||
| return Promise.reject(fetchResponse); | ||
| } else if (error.request) { | ||
| return Promise.reject(new Response(null, { status: 0, statusText: 'Network Error' })); | ||
| } else { | ||
| return Promise.reject(new Response(null, { status: 0, statusText: error.message })); | ||
| return new Response(response?.data, { | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| url: response.config.url, | ||
| headers: new Headers(Object.entries(response.headers ?? {})), | ||
| }); | ||
| } catch (err: any) { | ||
| if (err.response) { | ||
| return new Response(err.response?.data, { | ||
| status: err.status, | ||
| statusText: err.statusText, | ||
| headers: new Headers( | ||
| Object.entries(err.response.headers ?? {}) | ||
| ), | ||
| }); | ||
| } | ||
| return new Response(err.stack, { | ||
| status: err.status || 500, | ||
| statusText: err.message || "Internal Server Error", | ||
|
Amitkanswal marked this conversation as resolved.
|
||
| headers: new Headers(Object.entries(err.headers ?? {})), | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.