Skip to content

Commit c5fb10f

Browse files
fix(frontend): resolve fetched error message in file upload component
1 parent 8ec6aa7 commit c5fb10f

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 6 deletions
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 {
@@ -76,17 +77,19 @@ export const FileInputFieldController = class extends HTMLElement {
7677
this.$fileInputPath.readOnly = true;
7778

7879
const endpointResponse = await fetch(this.endpoint, {
79-
method: "POST",
8080
body: formData,
81+
method: "POST",
82+
headers: {
83+
Accept: "application/json",
84+
},
8185
});
8286

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

91+
this.$fileInputPath.value =
92+
await endpointResponse.headers.get("location");
9093
this.$fileInputPath.readOnly = false;
9194
this.$uploadProgress.hidden = true;
9295
} catch (error) {

packages/frontend/package.json

Lines changed: 1 addition & 0 deletions
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)