Skip to content

Commit

Permalink
fix: read json from blob-request error
Browse files Browse the repository at this point in the history
Fixes #171
  • Loading branch information
arildm committed Sep 24, 2024
1 parent 66f00b3 commit 6ad77ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ As this project is a user-facing application, the places in the semantic version
### Added

- Track some events to Matomo [#166](https://github.com/spraakbanken/mink-frontend/issues/166)
- Added `.editorconfig`

### Changed

Expand All @@ -25,8 +26,9 @@ As this project is a user-facing application, the places in the semantic version
### Fixed

- Show loading spinners on the custom config page
- Show loading spinners when downloading export files
- Strip slash from Matomo url
- Added `.editorconfig`
- Backend error messages are ignored when fetching files with "blob" as responseType [#171](https://github.com/spraakbanken/mink-frontend/issues/171)

## [1.6.1] (2024-08-13)

Expand Down
40 changes: 28 additions & 12 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import type {
ProgressHandler,
} from "@/api/api.types";

/** Handle an exception from an API call that may be encoded as Blob */
async function rethrowBlobError(error: any): Promise<never> {
if (error.response?.data instanceof Blob) {
// Parse JSON and replace the blob
const text = await error.response.data.text();
error.response.data = JSON.parse(text) as MinkResponse;
}
throw error;
}

/** Mink backend API client */
class MinkApi {
/** An instance of the Axios HTTP client. */
Expand Down Expand Up @@ -97,10 +107,12 @@ class MinkApi {

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Sources/operation/downloadsources */
async downloadSources(corpusId: string, filename: string, binary = false) {
const response = await this.axios.get<string | Blob>("download-sources", {
params: { corpus_id: corpusId, file: filename, zip: false },
responseType: binary ? "blob" : "text",
});
const response = await this.axios
.get<string | Blob>("download-sources", {
params: { corpus_id: corpusId, file: filename, zip: false },
responseType: binary ? "blob" : "text",
})
.catch(rethrowBlobError);
return response.data;
}

Expand Down Expand Up @@ -217,19 +229,23 @@ class MinkApi {

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Exports/operation/downloadexports */
async downloadExports(corpusId: string) {
const response = await this.axios.get<Blob>("download-exports", {
params: { corpus_id: corpusId },
responseType: "blob",
});
const response = await this.axios
.get<Blob>("download-exports", {
params: { corpus_id: corpusId },
responseType: "blob",
})
.catch(rethrowBlobError);
return response.data;
}

/** @see https://ws.spraakbanken.gu.se/ws/mink/api-doc#tag/Manage-Exports/operation/downloadexports */
async downloadExportFile(corpusId: string, path: string) {
const response = await this.axios.get<Blob>("download-exports", {
params: { corpus_id: corpusId, file: path, zip: false },
responseType: "blob",
});
const response = await this.axios
.get<Blob>("download-exports", {
params: { corpus_id: corpusId, file: path, zip: false },
responseType: "blob",
})
.catch(rethrowBlobError);
return response.data;
}

Expand Down

0 comments on commit 6ad77ef

Please sign in to comment.