Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #49104] Add server response (xhr) to direct-upload:error event. #49956

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions activestorage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
* Add `xhr` object to direct-upload:error event so that server generated error messages are accessible.

Before:
```javascript
addEventListener("direct-upload:error", event => {
event.preventDefault()
const { id, error } = event.detail
const element = document.getElementById(`direct-upload-${id}`)
element.classList.add("direct-upload--error")
element.setAttribute("title", error)
})
```

After:
```javascript
addEventListener("direct-upload:error", event => {
event.preventDefault()
const { id, error, xhr } = event.detail
const element = document.getElementById(`direct-upload-${id}`)
const errorMessage = xhr.response['error'] // Example: File size must be less than 100MB
element.classList.add("direct-upload--error")
element.setAttribute("title", errorMessage)
})
```

Fixes #49104

*Sean Abrahams*

* Allow accepting `service` as a proc as well in `has_one_attached` and `has_many_attached`.

*Yogesh Khater*
Expand Down
13 changes: 10 additions & 3 deletions activestorage/app/assets/javascripts/activestorage.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions activestorage/app/assets/javascripts/activestorage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion activestorage/app/javascript/activestorage/blob_record.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export class BlobRecord {
}

requestDidError(event) {
this.callback(`Error creating Blob for "${this.file.name}". Status: ${this.status}`)
this.callback({
xhr: this.xhr,
message: `Error creating Blob for "${this.file.name}". Status: ${this.status}`
})
}

toJSON() {
Expand Down
5 changes: 4 additions & 1 deletion activestorage/app/javascript/activestorage/blob_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export class BlobUpload {
}

requestDidError(event) {
this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`)
this.callback({
xhr: this.xhr,
message: `Error storing "${this.file.name}". Status: ${this.xhr.status}`
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class DirectUploadController {
}

dispatchError(error) {
const event = this.dispatch("error", { error })
const event = this.dispatch("error", { error: error.message, xhr: error.xhr })
if (!event.defaultPrevented) {
alert(error)
}
Expand Down