Skip to content

Commit dc6174a

Browse files
fix(frontend): resolve fetched error message in file upload component
1 parent 1a4d0ad commit dc6174a

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/frontend/components/file-input/index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IndiekitError } from "@indiekit/error";
12
import { wrapElement } from "../../lib/utils/wrap-element.js";
23

34
export const FileInputFieldController = class extends HTMLElement {
@@ -8,6 +9,7 @@ export const FileInputFieldController = class extends HTMLElement {
89

910
/** @type {HTMLElement} */
1011
this.$uploadProgress = this.querySelector(".file-input__progress");
12+
/** @type {HTMLInputElement} */
1113
this.$fileInputPath = this.querySelector(".file-input__path");
1214
this.$fileInputPicker = this.querySelector(".file-input__picker");
1315
this.$fileInputPickerTemplate = this.querySelector("#file-input-picker");
@@ -76,21 +78,23 @@ export const FileInputFieldController = class extends HTMLElement {
7678
this.$fileInputPath.readOnly = true;
7779

7880
const endpointResponse = await fetch(this.endpoint, {
79-
method: "POST",
8081
body: formData,
82+
method: "POST",
83+
headers: {
84+
Accept: "application/json",
85+
},
8186
});
8287

83-
if (endpointResponse.ok) {
84-
this.$fileInputPath.value =
85-
await endpointResponse.headers.get("location");
86-
} else {
87-
this.showErrorMessage(endpointResponse.statusText);
88+
if (!endpointResponse.ok) {
89+
throw await IndiekitError.fromFetch(endpointResponse);
8890
}
8991

92+
this.$fileInputPath.value =
93+
await endpointResponse.headers.get("location");
9094
this.$fileInputPath.readOnly = false;
9195
this.$uploadProgress.hidden = true;
9296
} catch (error) {
93-
this.showErrorMessage(error);
97+
this.showErrorMessage(error.message);
9498
this.$fileInputPath.readOnly = false;
9599
this.$uploadProgress.hidden = true;
96100
}

packages/frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"dependencies": {
3939
"@accessible-components/tag-input": "^0.2.0",
40+
"@indiekit/error": "^1.0.0-beta.8",
4041
"@indiekit/util": "^1.0.0-beta.10",
4142
"@rollup/plugin-commonjs": "^25.0.3",
4243
"@rollup/plugin-node-resolve": "^15.0.0",

0 commit comments

Comments
 (0)