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

Added JS exception dialog for failed ajax calls #5273

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion core/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,19 @@ a.bookmarklet { background-color:#ddd; border:1px solid #ccc; padding:5px;paddin
#oc-dialog-filepicker-content .filepicker_element_selected { background-color:lightblue;}
.ui-dialog {position:fixed !important;}
span.ui-icon {float: left; margin: 3px 7px 30px 0;}

#oc-dialog-exception-content{
overflow: auto;
/* IE8 will ignore this and will show horizontal scrollbars */
word-break: break-word;
}
#oc-dialog-exception-content .label{
font-weight: bold;
display: inline-block;
width: 50px;
}
#oc-dialog-exception-content .value{
color: #666;
}
.loading { background: url('../img/loading.gif') no-repeat center; cursor: wait; }
.move2trash { /* decrease spinner size */
width: 16px;
Expand Down
10 changes: 10 additions & 0 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,16 @@ function fillWindow(selector) {
}

$(document).ready(function(){
// catch ajax errors and display an exception dialog
$(document).bind('ajaxError', function(event, jqXHR, ajaxSettings, thrownError){
var exception = jqXHR.responseJSON && jqXHR.responseJSON.exception;
if (!exception){
return;
}
console.error(t('core', 'Server-side exception'), exception);
OC.dialogs.exception(exception, ajaxSettings);
});

sessionHeartBeat();

if(!SVGSupport()){ //replace all svg images with png images for browser that dont support svg
Expand Down
95 changes: 95 additions & 0 deletions core/js/oc-dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,85 @@ var OCdialogs = {
}
});
},
exception: function(exception, ajaxSettings, callback){
var self = this;
$.when(this._getExceptionTemplate()).then(function($tmpl) {
var dialog_name = 'oc-dialog-exception-content';
var dialog_id = '#' + dialog_name;
var url = '';
var message = '';
var stack = null;
if(self.$exceptionDialog) {
self.$exceptionDialog.ocdialog('close');
}

if (ajaxSettings){
url = ajaxSettings.type + ' ' + ajaxSettings.url;
}

if (exception.code){
message = '[' + exception.code + '] ';
}
message += exception.message;

self.$exceptionDialog = $tmpl.octemplate({
dialog_name: dialog_name,
title: t('core', 'Server-side exception'),
message: message,
url: url,
data: ajaxSettings && ajaxSettings.data,
stack: exception.stack,
hint: exception.hint
});

if (!ajaxSettings){
self.$exceptionDialog.find('.connection').remove();
}

if (!exception.stack){
self.$exceptionDialog.find('.stack').remove();
}

if (!exception.hint){
self.$exceptionDialog.find('.hint').remove();
}

$('body').append(self.$exceptionDialog);

var functionToCall = function() {
$(dialog_id).ocdialog('close');
if(callback !== undefined) {
callback();
}
};
var buttonlist = [{
text: t('core', 'Ok'),
click: functionToCall,
defaultButton: true
}];

self.$exceptionDialog.ocdialog({
closeOnEscape: true,
width: 800,
height: exception.stack?420:200,
modal: true,
buttons: buttonlist,
close: function(event, ui) {
try {
$(this).ocdialog('destroy').remove();
} catch(e) {}
self.$exceptionDialog = null;
}
});
})
.fail(function(status, error) {
// If the method is called while navigating away
// from the page, it is probably not needed ;)
if(status !== 0) {
alert(t('core', 'Error loading exception template: {error}', {error: error}));
}
});
},
_fileexistsshown: false,
/**
* Displays file exists dialog
Expand Down Expand Up @@ -476,6 +555,22 @@ var OCdialogs = {
}
return defer.promise();
},
_getExceptionTemplate: function() {
var defer = $.Deferred();
if(!this.$exceptionTemplate) {
var self = this;
$.get(OC.filePath('core', 'templates', 'exception.html'), function(tmpl) {
self.$exceptionTmpl = $(tmpl);
defer.resolve(self.$exceptionTmpl);
})
.fail(function(jqXHR, textStatus, errorThrown) {
defer.reject(jqXHR.status, errorThrown);
});
} else {
defer.resolve(this.$exceptionTmpl);
}
return defer.promise();
},
_getMessageTemplate: function() {
var defer = $.Deferred();
if(!this.$messageTemplate) {
Expand Down
13 changes: 13 additions & 0 deletions core/templates/exception.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div id="{dialog_name}" title="{title}">
<div><span class="label">Error: </span><span class="value">{message}</span></div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing translation of Error and below Data and Stack.

<div class="connection">
<div><span class="label">URL: </span><span class="value">{url}</span></div>
<div><span class="label">Data: </span><span class="value">{data}</span></div>
</div>
<div class="hint"><span class="label">Hint: </span><span class="value">{hint}</span></div>
<div class="stack">
<div class="label">Stack: </div>
<pre class="value">{stack}</pre>
</ul>
</div>
</div>
7 changes: 6 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@
//show the user a detailed error page
\OCP\Util::writeLog('index', $ex->getMessage(), \OCP\Util::FATAL);
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
OC_Template::printExceptionErrorPage($ex);
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm - I would have checked for the Accept header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, assuming that all browsers send it consistently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like jquery doesn't automatically send it. I'll need to find a way to set it globally for all ajax calls without too much hacking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like jquery doesn't automatically send it.

sometimes I'm really thinking about switching the job ....

I'll need to find a way to set it globally for all ajax calls without too much hacking.

yes - leave it the way it is

OC_Template::printExceptionJson($ex);
}
else{
OC_Template::printExceptionErrorPage($ex);
}
}
28 changes: 27 additions & 1 deletion lib/private/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public static function printExceptionErrorPage(Exception $exception) {
$hint = '<pre>'.$hint.'</pre>';
}
$l = OC_L10N::get('lib');
while (method_exists($exception, 'previous') && $exception = $exception->previous()) {
while (method_exists($exception, 'getPrevious') && $exception = $exception->getPrevious()) {
$error_msg .= '<br/>'.$l->t('Caused by:').' ';
if ($exception->getCode()) {
$error_msg .= '['.$exception->getCode().'] ';
Expand All @@ -308,4 +308,30 @@ public static function printExceptionErrorPage(Exception $exception) {
}
self::printErrorPage($error_msg, $hint);
}

public static function printExceptionJson(Exception $exception){
$message = $exception->getMessage();
$exceptionJson = array(
'code' => $exception->getCode()
);
if (defined('DEBUG') and DEBUG) {
$exceptionJson['stack'] = $exception->getTraceAsString();
// include cause
$l = OC_L10N::get('lib');
while (method_exists($exception, 'getPrevious') && $exception = $exception->getPrevious()) {
$message .= ' - '.$l->t('Caused by:').' ';
if ($exception->getCode()) {
$message .= '['.$exception->getCode().'] ';
}
$message .= $exception->getMessage() . "\n";
};
}
else{
if ($exception instanceof \OC\HintException){
$exceptionJson['hint'] = $exception->getHint();
}
}
$exceptionJson['message'] = $message;
\OCP\JSON::encodedPrint(array('exception' => $exceptionJson));
}
}