Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Uppy Error Messages

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
vario
Posts: 148
Joined: Mon Dec 05, 2011 12:23 pm
Location: Newton Abbot, UK
Has thanked: 1 time
Been thanked: 1 time

Uppy Error Messages

Unread post by vario »

I can't get my error messages back to the form when using the Uppy procedures.

I don't understand why the "catch" has "\Throwable" instead of "Exception", and I have tried removing the backslash (which I suspect is not meant to be there) and replacing it with "Exception" but none of the messages I set in my procedure are shown on the form where the upload button is.
I am using

Code: Select all

throw new Exception("My custom error message");
and I have moved the tests on allowed extension and max filesize into the "try" block.

Am I missing something here? I am trying to get my head around the Uppy docs but it is a little beyond my Javascript skill level at the moment!
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Uppy Error Messages

Unread post by kev1n »

Declare a function nuOnFileUploadComplete() in your form's Custom Code to retrieve the files that were uploaded successfully and the files that did not upload successfully

Code: Select all

function nuOnFileUploadComplete(source, id, result) {
	if (result.successful.length > 0) {
		console.log('Successful:')
		result.failed.forEach((file) => {
			// process successfully uploaded files
		})
	} else {

		if (result.failed.length > 0) {
			console.log('Errors:')
			result.failed.forEach((file) => {
				console.log(file.response.body.message)
			})
		}

	}

}
vario
Posts: 148
Joined: Mon Dec 05, 2011 12:23 pm
Location: Newton Abbot, UK
Has thanked: 1 time
Been thanked: 1 time

Re: Uppy Error Messages

Unread post by vario »

Thanks for the help, I have finally managed to get some time to look at this, I still struggle with understanding the whole Uppy thing, but here is how I got what I wanted:

1) Moved file size & extension checks into the try block, throwing exceptions as needed.

Code: Select all

// Allowed file extensions
$allowed = array('txt','csv');

// Maximum file size
$maxfilesize = 5 * 1024 * 1024; // (5 MB)

$filename = nuSanitizeFilename(basename($_FILES['file']['name']));
$target_dir = '/forte/upload/files/';
$target_file = $target_dir . $filename;

$error = nuTranslate('Sorry, there was an error uploading your file.');

try {

 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
 if (!in_array($ext, $allowed)) {
  throw new Exception('Invalid file type .' . $ext);
 }

 $filesize = $_FILES["file"]["size"];
 if ($filesize > $maxfilesize) {
  throw new Exception('Maximum file size 5MB exceeded');
 }

 if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
   // Add custom processing here...
  $data = ['url' => $target_file, 'file' => $filename, 'message' => 'The file ' . $filename . ' has been uploaded.'];
  http_response_code(201);
  $result = json_encode($data);
 } else {
    throw new Exception('Unable to move the uploaded file to its final location:' . $target_file);
 }
 
} catch (Throwable $th) {
 $data = ['message' => $error, 'error' => $th->getMessage()];
 $result = json_encode($data);
}
2) Used function nuOnFileUploadComplete as above, but with typo fixed(!) and nuMessage to relay error messages to user:

Code: Select all

function nuOnFileUploadComplete(source, id, result) {
 if (result.successful.length > 0) {
  console.log('Successful:')
  result.successful.forEach((file) => {
    console.log(file.response.body.message)
  })
 } else {
  if (result.failed.length > 0) {
   console.log('Errors:')
   result.failed.forEach((file) => {
    nuMessage(file.response.body.message + '<br>' + file.response.body.error)
   })
  }
 }
}
It would be cool if I could work out how to get Uppy to display my custom error messages in the upload box rather than just a bland "Upload error", and I suppose it is misleading to have Uppy report an upload failure when actually the file uploaded fine, it just failed a post-upload check.

Any criticisms or suggestion gratefully received!

Neil.
vario
Posts: 148
Joined: Mon Dec 05, 2011 12:23 pm
Location: Newton Abbot, UK
Has thanked: 1 time
Been thanked: 1 time

Re: Uppy Error Messages

Unread post by vario »

I have another problem now which I raise here as it follows on from the above.

Uppy seems to be failing to return the $result variable on successful completion. It returns errors raised via exceptions, but not the success outcome. Everything in the PHP is working, what happens is the function nuOnFileUploadComplete is finding file.response.body.message to be undefined on successful uploads.

**Update: Please ignore this! The undefined message is a result of loop overrun, not a bug...

Neil
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Uppy Error Messages

Unread post by kev1n »

vario wrote: Fri Dec 30, 2022 5:15 pm It would be cool if I could work out how to get Uppy to display my custom error messages in the upload box rather than just a bland "Upload error", and I suppose it is misleading to have Uppy report an upload failure when actually the file uploaded fine, it just failed a post-upload check.
The closest I could find was this post:
https://community.transloadit.com/t/add ... rd/15153/6
Post Reply