IIRC there's a OnError and a OnFailure event handler for the Request.JSON
class: the one is for catching connection timeouts, while the other can
catch parse errors, including 40x return code responses.

...

quick check: yep, used it that way in mootools-filemanager.
It's a bit tricky is the one originates from Request while the other
originates from Request.JSON if you dig in and RTFC of mootools. That's how
I found out about this earlier, anyway. :-)

(relevant code snippets at bottom of this message.)

HTH



On Wed, Jul 6, 2011 at 11:22 PM, Rolf -nl <[email protected]> wrote:

> Working with some api that can return a bad request code 400 error
> with the error message as a response in json. Is there a way I can use
> it? Because the response contains useful information about wrongly
> used params? There's a failure event, but that works with the timeout
> and I that's not really usable or the same.
>



...

cvtXHRerror2msg: function(xmlHttpRequest) {
var status = xmlHttpRequest.status;
var orsc = xmlHttpRequest.onreadystatechange;
var response = (xmlHttpRequest.responseText ||
this.language['backend.unidentified_error']);

var text = response.substitute(this.language, /\\?\$\{([^{}]+)\}/g);
return text;
},

....

// filemanager.request is just an slightly augmented Request.JSON derivative
class;
// filebrowser.showError() shows the human-readable error response, when one
occurs.


FileManager.Request = new Class({
Extends: Request.JSON,

options:
{
secure:          true, // Isn't this true by default anyway in REQUEST.JSON?
fmDisplayErrors: true  // Automatically display errors - ** your onSuccess
still gets called, just ignore if it's an error **
},

initialize: function(options, filebrowser) {
this.parent(options);

this.options.data = Object.merge({}, filebrowser.options.propagateData,
this.options.data);

if (this.options.fmDisplayErrors)
{
this.addEvents({
success: function(j)
{
if (!j)
{
filebrowser.showError();
}
else if (!j.status)
{
filebrowser.showError(('' + j.error).substitute(filebrowser.language,
/\\?\$\{([^{}]+)\}/g));
}
}.bind(this),

error: function(text, error)
{
filebrowser.showError(text);
},

failure: function(xmlHttpRequest)
{
var text = filebrowser.cvtXHRerror2msg(xmlHttpRequest);
filebrowser.showError(text);
}
});
}

this.addEvents({
request: filebrowser.onRequest.bind(filebrowser),
complete: filebrowser.onComplete.bind(filebrowser),
success: filebrowser.onSuccess.bind(filebrowser),
error: filebrowser.onError.bind(filebrowser),
failure: filebrowser.onFailure.bind(filebrowser)
});
}
});



Server-side PHP has blurbs like these to send 'useful' error texts with 40x
responses, here a 403:

/**
 * Process the 'download' event
 *
 * Send the file content of the specified file for download by the client.
 * Only files residing within the directory tree rooted by the
 * 'basedir' (options['URLpath4FileManagedDirTree']) will be allowed to be
downloaded.
 *
 * Expected parameters:
 *
 * $_POST['file']         filepath of the file to be downloaded
 *
 * $_POST['filter']       optional mimetype filter string, amy be the part
up to and
 *                        including the slash '/' or the full mimetype. Only
files
 *                        matching this (set of) mimetypes will be listed.
 *                        Examples: 'image/' or 'application/zip'
 *
 * On errors a HTTP 403 error response will be sent instead.
 */
protected function onDownload()
{
$emsg = null;
$file_arg = null;
$file = null;
$jserr = array(
'status' => 1
);

try
{
if (!$this->options['download'])
throw new FileManagerException('disabled:download');

[... doing stuff ...]
$this->sendHttpHeaders($hdrs);

fpassthru($fd);
fclose($fd);
return;
}

$emsg = 'read_error';
}
catch(FileManagerException $e)
{
$emsg = $e->getMessage();
}
catch(Exception $e)
{
// catching other severe failures; since this can be anything and should
only happen in the direst of circumstances, we don't bother translating
$emsg = $e->getMessage();
}

// we don't care whether it's a 404, a 403 or something else entirely: we
feed 'em a 403 and that's final!
send_response_status_header(403);

$this->modify_json4exception($jserr, $emsg, 'file = ' .
$this->mkSafe4Display($file_arg . ', destination path = ' . $file));

$this->sendHttpHeaders('Content-Type: text/plain');        // Safer for
iframes: the 'application/json' mime type would cause FF3.X to pop up a
save/view dialog when transmitting these error reports!

// when we fail here, it's pretty darn bad and nothing to it.
// just push the error JSON and go.
echo json_encode($jserr);
}



-- 
Met vriendelijke groeten / Best regards,

Ger Hobbelt

--------------------------------------------------
web:    http://www.hobbelt.com/
        http://www.hebbut.net/
mail:   [email protected]
mobile: +31-6-11 120 978
--------------------------------------------------

Reply via email to