-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1694 from silx-kit/refact-api
Remove axios from `DataProviderApi` base class and refactor axios error handling
- Loading branch information
Showing
7 changed files
with
203 additions
and
188 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -9,11 +9,12 @@ import type { | |
} from '@h5web/shared/hdf5-models'; | ||
import { DTypeClass } from '@h5web/shared/hdf5-models'; | ||
import type { OnProgress } from '@h5web/shared/react-suspense-fetch'; | ||
import type { AxiosRequestConfig } from 'axios'; | ||
import type { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
import axios, { AxiosError } from 'axios'; | ||
|
||
import { DataProviderApi } from '../api'; | ||
import type { ExportFormat, ExportURL, ValuesStoreParams } from '../models'; | ||
import { handleAxiosError } from '../utils'; | ||
import { createAxiosProgressHandler } from '../utils'; | ||
import type { | ||
H5GroveAttrValuesResponse, | ||
H5GroveDataResponse, | ||
|
@@ -22,19 +23,27 @@ import type { | |
} from './models'; | ||
import { | ||
h5groveTypedArrayFromDType, | ||
hasErrorMessage, | ||
isH5GroveError, | ||
parseEntity, | ||
} from './utils'; | ||
|
||
export class H5GroveApi extends DataProviderApi { | ||
private readonly client: AxiosInstance; | ||
|
||
/* API compatible with [email protected] */ | ||
public constructor( | ||
url: string, | ||
filepath: string, | ||
axiosConfig?: AxiosRequestConfig, | ||
private readonly _getExportURL?: DataProviderApi['getExportURL'], | ||
) { | ||
super(filepath, { adapter: 'fetch', baseURL: url, ...axiosConfig }); | ||
super(filepath); | ||
|
||
this.client = axios.create({ | ||
adapter: 'fetch', | ||
baseURL: url, | ||
...axiosConfig, | ||
}); | ||
} | ||
|
||
public override async getEntity(path: string): Promise<ProvidedEntity> { | ||
|
@@ -49,25 +58,38 @@ export class H5GroveApi extends DataProviderApi { | |
): Promise<H5GroveDataResponse> { | ||
const { dataset } = params; | ||
|
||
if (dataset.type.class === DTypeClass.Opaque) { | ||
return new Uint8Array( | ||
await this.fetchBinaryData(params, signal, onProgress), | ||
); | ||
} | ||
|
||
const DTypedArray = h5groveTypedArrayFromDType(dataset.type); | ||
if (DTypedArray) { | ||
const buffer = await this.fetchBinaryData( | ||
params, | ||
signal, | ||
onProgress, | ||
true, | ||
); | ||
const array = new DTypedArray(buffer); | ||
return hasScalarShape(dataset) ? array[0] : array; | ||
try { | ||
if (dataset.type.class === DTypeClass.Opaque) { | ||
return new Uint8Array( | ||
await this.fetchBinaryData(params, signal, onProgress), | ||
); | ||
} | ||
|
||
const DTypedArray = h5groveTypedArrayFromDType(dataset.type); | ||
if (DTypedArray) { | ||
const buffer = await this.fetchBinaryData( | ||
params, | ||
signal, | ||
onProgress, | ||
true, | ||
); | ||
const array = new DTypedArray(buffer); | ||
return hasScalarShape(dataset) ? array[0] : array; | ||
} | ||
|
||
return await this.fetchData(params, signal, onProgress); | ||
} catch (error) { | ||
if (error instanceof AxiosError && axios.isCancel(error)) { | ||
// Throw abort reason instead of axios `CancelError` | ||
// https://github.com/axios/axios/issues/5758 | ||
throw new Error( | ||
typeof signal?.reason === 'string' ? signal.reason : 'cancelled', | ||
{ cause: error }, | ||
); | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
return this.fetchData(params, signal, onProgress); | ||
} | ||
|
||
public override async getAttrValues( | ||
|
@@ -114,32 +136,40 @@ export class H5GroveApi extends DataProviderApi { | |
} | ||
|
||
private async fetchEntity(path: string): Promise<H5GroveEntityResponse> { | ||
const { data } = await handleAxiosError( | ||
() => | ||
this.client.get<H5GroveEntityResponse>(`/meta/`, { params: { path } }), | ||
(_, errorData) => { | ||
if (!hasErrorMessage(errorData)) { | ||
return undefined; | ||
} | ||
const { message } = errorData; | ||
|
||
if (message.includes('File not found')) { | ||
return `File not found: '${this.filepath}'`; | ||
} | ||
if (message.includes('Permission denied')) { | ||
return `Cannot read file '${this.filepath}': Permission denied`; | ||
} | ||
if (message.includes('not a valid path')) { | ||
return `No entity found at ${path}`; | ||
} | ||
if (message.includes('Cannot resolve')) { | ||
return `Could not resolve soft link at ${path}`; | ||
} | ||
|
||
return undefined; | ||
}, | ||
); | ||
return data; | ||
try { | ||
const { data } = await this.client.get<H5GroveEntityResponse>(`/meta/`, { | ||
params: { path }, | ||
}); | ||
return data; | ||
} catch (error) { | ||
if ( | ||
!(error instanceof AxiosError) || | ||
!isH5GroveError(error.response?.data) | ||
) { | ||
throw error; | ||
} | ||
|
||
const { message } = error.response.data; | ||
if (message.includes('File not found')) { | ||
throw new Error(`File not found: '${this.filepath}'`, { cause: error }); | ||
} | ||
if (message.includes('Permission denied')) { | ||
throw new Error( | ||
`Cannot read file '${this.filepath}': Permission denied`, | ||
{ cause: error }, | ||
); | ||
} | ||
if (message.includes('not a valid path')) { | ||
throw new Error(`No entity found at ${path}`, { cause: error }); | ||
} | ||
if (message.includes('Cannot resolve')) { | ||
throw new Error(`Could not resolve soft link at ${path}`, { | ||
cause: error, | ||
}); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
private async fetchAttrValues( | ||
|
@@ -154,42 +184,38 @@ export class H5GroveApi extends DataProviderApi { | |
|
||
private async fetchData( | ||
params: ValuesStoreParams, | ||
signal?: AbortSignal, | ||
onProgress?: OnProgress, | ||
signal: AbortSignal | undefined, | ||
onProgress: OnProgress | undefined, | ||
): Promise<H5GroveDataResponse> { | ||
const { data } = await this.cancellableFetchValue( | ||
`/data/`, | ||
{ | ||
const { data } = await this.client.get<H5GroveDataResponse>('/data/', { | ||
params: { | ||
path: params.dataset.path, | ||
selection: params.selection, | ||
flatten: true, | ||
}, | ||
signal, | ||
onProgress, | ||
); | ||
|
||
onDownloadProgress: createAxiosProgressHandler(onProgress), | ||
}); | ||
return data; | ||
} | ||
|
||
private async fetchBinaryData( | ||
params: ValuesStoreParams, | ||
signal?: AbortSignal, | ||
onProgress?: OnProgress, | ||
signal: AbortSignal | undefined, | ||
onProgress: OnProgress | undefined, | ||
safe = false, | ||
): Promise<ArrayBuffer> { | ||
const { data } = await this.cancellableFetchValue( | ||
'/data/', | ||
{ | ||
const { data } = await this.client.get<ArrayBuffer>('/data/', { | ||
responseType: 'arraybuffer', | ||
params: { | ||
path: params.dataset.path, | ||
selection: params.selection, | ||
format: 'bin', | ||
dtype: safe ? 'safe' : undefined, | ||
}, | ||
signal, | ||
onProgress, | ||
'arraybuffer', | ||
); | ||
|
||
onDownloadProgress: createAxiosProgressHandler(onProgress), | ||
}); | ||
return data; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.