# Personalizzare le pagine 404 e 401

> Registra gestori ROUTE('#404') e ROUTE('#401') per sostituire le pagine di errore integrate e attivali tu stesso con $.fallback().

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: it
Content-SHA256: bd8205ad9de5371ce80f4c5900f269f6ad38e64f9c28fad18e8aae82e37e8103
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Quando Total.js non può servire una richiesta mostra una pagina di errore integrata. Una rotta il cui URL è `#` più il codice di stato la sostituisce: `ROUTE('#404', fn)` per "non trovato", `ROUTE('#401', fn)` per "non autorizzato", e lo stesso per `#400`, `#431` e altri. Il gestore riceve `$` come qualsiasi rotta, ma **lo stato lo imposti tu** con `$.response.status`: Total.js non lo fa al posto tuo quando esiste un gestore personalizzato. Senza un gestore personalizzato si usa la pagina integrata e le richieste AJAX (header `x-requested-with`) ricevono un errore JSON invece di HTML.

`$.fallback(code)` invia la richiesta corrente al gestore per `code`, ed è il modo per rispondere 404 per un record inesistente o 403 per una risorsa vietata dal tuo codice.

```js controllers/errors.js
exports.install = function() {
	ROUTE('#404', not_found);
	ROUTE('#401', unauthorized);
	ROUTE('GET /items/{id}/', item);
	ROUTE('+GET /private/', private_page);
};

var items = { '1': 'First item' };

function item($) {
	var name = items[$.params.id];
	if (name)
		$.json({ name: name });
	else
		$.fallback(404);
}

function private_page($) {
	$.plain('secret');
}

function not_found($) {
	$.response.status = 404;
	$.html('<h1>Nothing here</h1><p>' + $.url.encode() + ' does not exist.</p>');
}

function unauthorized($) {
	$.response.status = 401;
	$.json({ error: 'Please sign in first' });
}

AUTH(function($) {
	$.invalid();
});
```
