Skip to content

Commit

Permalink
fix: manage soap errors (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
nubsthead authored Oct 11, 2022
1 parent cf4ca25 commit b26ed75
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
24 changes: 10 additions & 14 deletions src/network/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@

import { find, map, maxBy } from 'lodash';
import { goToLogin } from './go-to-login';
import { Account, ErrorSoapResponse, SoapContext, SoapResponse } from '../../types';
import {
Account,
ErrorSoapBodyResponse,
ErrorSoapResponse,
SoapContext,
SoapResponse
} from '../../types';
import { userAgent } from './user-agent';
import { report } from '../reporting';
import { useAccountStore } from '../store/account';
import { IS_STANDALONE, SHELL_APP_ID } from '../constants';
import { useNetworkStore } from '../store/network';
import { handleSync } from '../store/network/utils';

class SoapException extends Error {
constructor(response: ErrorSoapResponse) {
super(response?.Body.Fault.Reason.Text);
this.response = response;
}

response: ErrorSoapResponse;
}

export const noOp = (): void => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
getSoapFetch(SHELL_APP_ID)(
Expand Down Expand Up @@ -94,7 +91,7 @@ const normalizeContext = (context: any): SoapContext => {
return context;
};

const handleResponse = <R>(api: string, res: SoapResponse<R>): R => {
const handleResponse = <R>(api: string, res: SoapResponse<R>): R | ErrorSoapBodyResponse => {
const { pollingInterval, noOpTimeout } = useNetworkStore.getState();
const { usedQuota } = useAccountStore.getState();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -120,8 +117,6 @@ const handleResponse = <R>(api: string, res: SoapResponse<R>): R => {
}`
)
);

throw new SoapException(<ErrorSoapResponse>res);
}
if (res.Header?.context) {
const responseUsedQuota =
Expand All @@ -143,9 +138,10 @@ const handleResponse = <R>(api: string, res: SoapResponse<R>): R => {
..._context
});
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return res.Body[`${api}Response`] as R;
return res?.Body?.Fault ? (res.Body as ErrorSoapBodyResponse) : (res.Body[`${api}Response`] as R);
};
export const getSoapFetch =
(app: string) =>
Expand Down
3 changes: 0 additions & 3 deletions types/exports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ export const soapFetch: SoapFetch;
export const xmlSoapFetch: SoapFetch;
export const report: (error: Error, hint?: unknown) => void;
export const setAppContext: <T>(obj: T) => void;
export class SoapException extends Error {
response: ErrorSoapResponse;
}

export const removeActions: (...ids: Array<string>) => void;
export const registerActions: (
Expand Down
11 changes: 8 additions & 3 deletions types/network/soap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { BaseFolder, LinkFolderFields, SearchFolderFields } from '../misc';
export type SoapHeader = {
context: SoapContext;
};

export type SuccessSoapResponse<R> = {
Body: Record<string, R>;
Header: SoapHeader;
};

export type SoapFault = {
Detail: {
Error: {
Expand All @@ -25,10 +27,13 @@ export type SoapFault = {
Text: string;
};
};

export type ErrorSoapBodyResponse = {
Fault: SoapFault;
};

export type ErrorSoapResponse = {
Body: {
Fault: SoapFault;
};
Body: ErrorSoapBodyResponse;
Header: SoapHeader;
};

Expand Down

0 comments on commit b26ed75

Please sign in to comment.