-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Don't send errors as HTML if request looks like a json request #15357
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,20 @@ | |
|
||
//show the user a detailed error page | ||
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); | ||
OC_Template::printExceptionErrorPage($ex); | ||
if (\OC::$server->getRequest()->isJSONRequest()) { | ||
OC_Template::printExceptionErrorPage($ex); | ||
} | ||
} catch (\OC\HintException $ex) { | ||
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); | ||
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint()); | ||
if (\OC::$server->getRequest()->isJSONRequest()) { | ||
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint()); | ||
} | ||
} catch (Exception $ex) { | ||
\OCP\Util::logException('index', $ex); | ||
|
||
//show the user a detailed error page | ||
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); | ||
OC_Template::printExceptionErrorPage($ex); | ||
if (\OC::$server->getRequest()->isJSONRequest()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be inverted - right? If it's NOT a json request the exception page is displayed |
||
OC_Template::printExceptionErrorPage($ex); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -758,4 +758,18 @@ private function getOverwriteHost() { | |
return null; | ||
} | ||
|
||
/** | ||
* Checks whether the request originates from jQuery | ||
* or if the Accept Header contains application json | ||
* @return boolean true if at least one of the headers is set, false otherwise | ||
*/ | ||
public function isJSONRequest() { | ||
if (stristr($this->server['HTTP_X_REQUEST_WITH'], 'XMLHttpRequest') | ||
|| stristr($this->server['HTTP_ACCEPT'],'application/json') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this work for use out of the box? We send no accept headers in our ajax calls as of now - what we probably should do. relying on X_REQUEST_WITH seems a bit fragile to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jQuery sends it by default. Nearly no one sends the |
||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about moving this check inside printExceptionErrorPage() adn even send back the error message as json?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this looks ugly. But as a developer you expect printExceptionErrorPage() to output something ... it starts with print.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case of json request it will print out json - on cli it will print out plain text - html for all the rest