Fix: Add missing _uploadErrored method with onUploadError lifecycle hook#214
Conversation
Co-authored-by: grantcopley <1197835+grantcopley@users.noreply.github.com>
Co-authored-by: grantcopley <1197835+grantcopley@users.noreply.github.com>
Co-authored-by: grantcopley <1197835+grantcopley@users.noreply.github.com>
| // Check if the component has an onUploadError method and invoke it | ||
| if ( structKeyExists( this, "onUploadError" ) ) { | ||
| invoke( this, "onUploadError", { | ||
| name: arguments.prop, |
There was a problem hiding this comment.
@copilot I'd like to change the name argument to instead be property
There was a problem hiding this comment.
Changed the parameter from name to property in the onUploadError lifecycle method (commit 8e4e2d9).
Co-authored-by: grantcopley <1197835+grantcopley@users.noreply.github.com>
|
@copilot Can you check livewire.js and look to see if when an error does occur during file upload, are we suppose to return a certain payload with the error information? |
Co-authored-by: grantcopley <1197835+grantcopley@users.noreply.github.com>
Yes, the JavaScript has a listener for |
|
@copilot One final thing, fully document the feature here in a comment where I can paste it into our documentation site please. |
Here's the complete documentation for the onUploadError Lifecycle HookThe Method Signaturefunction onUploadError( property, errors, multiple )Parameters
Usage Examplecomponent extends="cbwire.models.Component" {
data = {
"photo": "",
"uploadFailed": false,
"errorMessage": ""
};
function onUploadError( property, errors, multiple ) {
// Set error state
data.uploadFailed = true;
// Create user-friendly error message
data.errorMessage = "Failed to upload " & ( multiple ? "files" : "file" ) & " for " & property;
// Log the error for debugging
if ( !isNull( errors ) ) {
writeLog( type="error", text="Upload error for #property#: #serializeJSON(errors)#" );
}
// You could also dispatch events, send notifications, etc.
}
}Template Example<cfoutput>
<div>
<input type="file" wire:model="photo">
<cfif data.uploadFailed>
<div class="alert alert-danger">
#data.errorMessage#
</div>
</cfif>
</div>
</cfoutput>When It's CalledThe
Error InformationThe
You can check the response status in your server logs or implement custom error handling based on your application's needs. Common Use Cases
Related Lifecycle Hooks
Notes
|
Problem
When file uploads encountered errors (HTTP non-2xx responses), CBWire was throwing a
CBWIREException:This occurred because the JavaScript code in
includes/js/livewire.jswas calling a non-existent method when upload errors occurred:Solution
This PR adds the missing
_uploadErrored()method toComponent.cfcand implements a newonUploadError()lifecycle hook that allows developers to handle upload errors in their components.Implementation
The new method follows the same pattern as other lifecycle methods (
onHydrate,onUpdate) and upload methods (_startUpload,_finishUpload):function _uploadErrored( prop, errors, multiple ) { // Dispatch the upload errored event dispatchSelf( event="upload:errored", params=[ "name"=arguments.prop ] ); // Check if the component has an onUploadError method and invoke it if ( structKeyExists( this, "onUploadError" ) ) { invoke( this, "onUploadError", { property: arguments.prop, errors: arguments.errors, multiple: arguments.multiple } ); } }The method dispatches the
upload:erroredevent to the JavaScript listener, which triggers the error callback handler, and optionally invokes the component'sonUploadError()method if defined.Usage
Developers can now handle upload errors by defining an
onUploadError()method in their CBWIRE components:component extends="cbwire.models.Component" { data = { "uploadFailed": false, "errorMessage": "" }; function onUploadError( property, errors, multiple ) { data.uploadFailed = true; data.errorMessage = "Upload failed for " & arguments.property; // errors contains the server response if status was 422 // multiple indicates if this was a multi-file upload } }Parameters
The lifecycle method receives three parameters with complete error context:
Tests
Added comprehensive test coverage:
onUploadError()implementationRelated Issue
Fixes the
CBWIREExceptionerror that occurred when file uploads failed.Module version affected: 4.1.5+
Fixes #178
Original prompt
Fixes #178
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.