17.2.3. Error handling
17.2.3.1. Backend
Any exception that needs to be reported to the user must implement interface
nextgisweb.core.exception.IUserException. It can be done via
subclassing exception from base exception classes from
nextgisweb.core.exception module or per object basis via
nextgisweb.core.exception.user_exception() method.
Exception can provide following attributes:
titleGeneral error description.
messageUser friendly and secure message describing error.
detailInformation about fixing problem in Web GIS context.
http_status_codeCorresponding HTTP 4xx or 5xx status code.
dataError specific JSON-serializable dictionary.
New exception classes can be inherited from:
UserExceptionBase class for any other exception below and custom exceptions.
ValidationErrorFor validation errors (HTTP status code is 422).
InsufficientPermissionsFor authorizations errors (HTTP status code is 403).
OperationalErrorFor operational errors (default HTTP status code is 503).
TODO: Add examples for user_exception().
17.2.3.2. Webserver and API
Pyramid component handles exception wich provides
IUserException and processes them with
following rules:
For API and XHR requests JSON representation returned. API requests identified by
/api/path and XHR requests identified byX-Requested-WithHTTP header.For other requests HTML representation returned. HTML representation is rendered by
nextgisweb:pyramid/template/error.makotemplate.
Exceptions that doesn’t support interface
IUserException wrapped into
nextgisweb.pyramin.exception.InternalServerError wich support this
interface, so they can be handle same way.
Titles, messages and details of exception are translated to request locate so you can use localized strings and i18n framework.
17.2.3.2.1. JSON representation
TODO: Add schema and example
17.2.3.3. Frontend
Сlass ngw-pyramid/ErrorDialog is designed to show modal dialog with an
error message:
require(["ngw-pyramid/ErrorDialog/ErrorDialog"], function (ErrorDialog) {
// ...
if (somethingWentWrong) {
new ErrorDialog({
title: i18n.gettext("Unexpected error"),
message: i18.gettext("Something went wrong."),
detail: i18.gettext("Please try again later.")
}).show()
}
})
ErrorDialog can also handle dojo/request/xhr errors:
require([
"dojo/request/xhr",
"ngw-pyramid/ErrorDialog"
], function (
xhr,
ErrorDialog
) {
xhr(API_URL, {
requestMethod: 'GET',
handleAs: 'json'
}).then(
function (data) { /* everything is alright */ },
ErrorDialog.xhrError
)
})