0

I'm trying to implement dropzone.js into my CakePHP application. So far it all went fine. Except When I receive an error, it displays the whole HTML error page, not rendered. Which turns into a bunch of HTML code, not quite readable and because the error box becomes so big I cannot click the "remove" button. See picture below:

As soon as I receive an error: enter image description here

When I hover the box, after receiving an error: enter image description here

I know the reason is that dropzone.js recognizes the error because of the 500 header of the Ajax page (I throw an Exception if something goes wrong). And CakePHP renders a complete layout for a 500 error page. So it's not possible for me to just view a one row error. And I really need the 500 header, because else dropzone.js thinks everything went fine....

So my question: Is it possible to NOT render the 500 error layout, when getting a 500 error within a specific Controller method? I don't want to completely disable the 500 error layout rendering. Only for AJAX pages.

public function admin_add($slug = null) {
    if(!$slug || !$client = $this->Video->Client->find('first', array('conditions' => array('slug' => $slug)))) {
        throw new NotFoundException(__('Invalid client'));
    }

    if ($this->request->is('post')) {

        // If request contains files, continue
        if (!empty($_FILES)) {
            // Get slug from URL
            $slug = substr( $this->referer(), strrpos( $this->referer(), '/' )+1 );

            // Create new folder for the movies if it doesn't exist already
            if (!file_exists(WWW_ROOT.'/files/'.$slug)) {
                mkdir(WWW_ROOT.'/files/'.$slug, 0777, true);
            }

            $tempFile = $_FILES['file']['tmp_name'];
            $targetPath = '/files/'.$slug.'/';  
            $targetFile =  $targetPath. $_FILES['file']['name']; 

            // Create variable filename without the extension
            $fileWithoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $_FILES['file']['name']);

            // Add file to Video array
            $video['Video'] = array('video' => $targetFile, 'screenshot' => '/files/'.$slug.'/screenshots/'.$fileWithoutExt.'.jpg', 'client_id' => $client['Client']['id']);
            // unset($video);
            // Try moving the file to their final directory
            if(!move_uploaded_file($tempFile, WWW_ROOT.$targetFile)) {
                throw new NotFoundException(__('Move image to "'.WWW_ROOT.$targetPath.'" failed'));
            } 

            // Create new folder for the screenshots if it doesn't exist already
            if (!file_exists(WWW_ROOT.'/files/'.$slug.'/screenshots/')) {
                mkdir(WWW_ROOT.'/files/'.$slug.'/screenshots/', 0777, true);
            }

            // Try saving video to Video table in the database
            if(!$this->Video->save($video)){
                throw new NotFoundException(__('Failed connecting client with "'.$targetFile.'" in the database'));
            }
        }
        $this->Session->setFlash(__('Videos successfully uploaded'), 'default', array(), 'success');
        $this->redirect($this->referer());
    }
    $title_for_layout = $client['Client']['name'];
    $this->set(compact('title_for_layout', 'client'));
}
Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
  • short answer: YES, it's possible. if you provide more source code, we can provide more concrete recommendations. – Vadim Feb 19 '14 at 17:31
  • Alright, I'll post the CakePHP method for adding the dropzone.js uploads to the database (this function creates the exceptions, and thus the 500 errors, as well) tomorrow, as soon as I'm at the office. – Erik van de Ven Feb 19 '14 at 21:37

1 Answers1

0
  1. You can change retuned status code by use statusCode method of CakeResponse class. Something like this: $this->response->statusCode(404);
  2. It's a little not correct to use NotFoundException to return http status code. At least you can create your own application exception

Please, check Creating your own application exceptions

You will easily to define one exceptions:

class MissingWidgetException extends CakeException {};

And after that you can use it and send http status code which you need Creating custom status codes :

throw new MissingWidgetHelperException('Its not here', 501);

501 is http status code.

Hope, this will help to find out right solution.

Vadim
  • 622
  • 3
  • 5
  • Thanks! So far it goes fine. Except I recognize I really need a 500 error, to prevent dropzone.js from finishing the upload. It only stops at a 500 error. So is there a possibility to create one custom 500 layout for just the current controlle method? I can change the 500.ctp in Errors, but I want that layout for other controller methods.... – Erik van de Ven Feb 20 '14 at 10:53
  • 1
    500 error it's just server return http status code 500, so, you can use `$this->response->statusCode(500);` and render that you really needed. More details about how to return some http status code you can find here: http://stackoverflow.com/questions/1100867/how-do-you-specify-an-http-status-code-in-cakephp/20757644 – Vadim Feb 20 '14 at 11:38
  • Alright! It works quite well, now :) I don't get the enormous bunch of code anymore :) Thanks for your help! – Erik van de Ven Feb 20 '14 at 11:58