Skip to content

Commit eeb8502

Browse files
authored
Merge pull request #3176 from tvdeyen/uploader-handle-html-errors
fix(Uploader): Handle HTML Errors during upload
2 parents 45de21c + 266ca1e commit eeb8502

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

app/javascript/alchemy_admin/components/uploader/file_upload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class FileUpload extends AlchemyHTMLElement {
200200
const response = JSON.parse(this.request.responseText)
201201
return response["message"]
202202
} catch (error) {
203-
return translate("Could not parse JSON result")
203+
return `${this.request.status}: ${this.request.statusText}`
204204
}
205205
}
206206

spec/javascript/alchemy_admin/components/uploader/file_upload.spec.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ describe("alchemy-file-upload", () => {
2323

2424
const testFile = new File(["a".repeat(1100)], "foo.txt")
2525

26-
const mockXMLHttpRequest = (status = 200, response = {}) => {
26+
const mockXMLHttpRequest = (
27+
status = 200,
28+
response = {},
29+
reason = "Created"
30+
) => {
31+
const body =
32+
typeof response === "string" ? response : JSON.stringify(response)
33+
2734
mock.setup()
2835
mock.post("/admin/pictures", {
2936
status,
30-
body: JSON.stringify(response)
37+
reason,
38+
body
3139
})
3240

3341
let request = new XMLHttpRequest()
@@ -203,26 +211,54 @@ describe("alchemy-file-upload", () => {
203211
})
204212

205213
describe("failed server response", () => {
206-
beforeEach(() => {
207-
const xhrMock = mockXMLHttpRequest(400, {
208-
message: "Error: Foo Bar"
214+
describe("with a JSON response", () => {
215+
beforeEach(() => {
216+
const xhrMock = mockXMLHttpRequest(400, {
217+
message: "Error: Foo Bar"
218+
})
219+
renderComponent(testFile, xhrMock)
220+
component.request.open("post", "/admin/pictures")
221+
component.request.send()
209222
})
210-
renderComponent(testFile, xhrMock)
211-
component.request.open("post", "/admin/pictures")
212-
component.request.send()
213-
})
214223

215-
it("should call the growl method", () => {
216-
expect(growl).toHaveBeenCalledWith("Error: Foo Bar", "error")
217-
expect(growl).toHaveBeenCalledTimes(1)
218-
})
224+
it("should call the growl method", () => {
225+
expect(growl).toHaveBeenCalledWith("Error: Foo Bar", "error")
226+
expect(growl).toHaveBeenCalledTimes(1)
227+
})
219228

220-
it("should mark as failed", () => {
221-
expect(component.className).toEqual("failed")
229+
it("should mark as failed", () => {
230+
expect(component.className).toEqual("failed")
231+
})
232+
233+
it("should have an error message", () => {
234+
expect(component.errorMessage).toEqual("Error: Foo Bar")
235+
})
222236
})
223237

224-
it("should have an error message", () => {
225-
expect(component.errorMessage).toEqual("Error: Foo Bar")
238+
describe("without a JSON response", () => {
239+
beforeEach(() => {
240+
const xhrMock = mockXMLHttpRequest(
241+
502,
242+
"<h1>Error</h1><p>Foo Bar</p>",
243+
"Bad Gateway"
244+
)
245+
renderComponent(testFile, xhrMock)
246+
component.request.open("post", "/admin/pictures")
247+
component.request.send()
248+
})
249+
250+
it("should call the growl method", () => {
251+
expect(growl).toHaveBeenCalledWith("502: Bad Gateway", "error")
252+
expect(growl).toHaveBeenCalledTimes(1)
253+
})
254+
255+
it("should mark as failed", () => {
256+
expect(component.className).toEqual("failed")
257+
})
258+
259+
it("should have an error message", () => {
260+
expect(component.errorMessage).toEqual("502: Bad Gateway")
261+
})
226262
})
227263
})
228264
})

0 commit comments

Comments
 (0)