From cf7fad79d7709b3f8b851a0d72018f16997b5f30 Mon Sep 17 00:00:00 2001 From: Javad Zobeidi Date: Mon, 3 Feb 2025 12:56:06 +0330 Subject: [PATCH] Add Redirect Exception --- .../authentication/redirect_if_authenticated.dart | 5 ++++- lib/src/exception/http_exception.dart | 13 ++++++++----- lib/src/exception/redirect_exception.dart | 12 ++++++++++++ lib/src/http/request/request_handler.dart | 4 ++++ lib/vania.dart | 1 + 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 lib/src/exception/redirect_exception.dart diff --git a/lib/src/authentication/redirect_if_authenticated.dart b/lib/src/authentication/redirect_if_authenticated.dart index 360ec44..6eea8ef 100644 --- a/lib/src/authentication/redirect_if_authenticated.dart +++ b/lib/src/authentication/redirect_if_authenticated.dart @@ -8,7 +8,10 @@ class RedirectIfAuthenticated extends Middleware { Future handle(Request req) async { bool loggedIn = await getSession('logged_in') ?? false; if (loggedIn) { - return Response.redirect(path); + throw RedirectException( + message: path, + responseType: ResponseType.html, + ); } } } diff --git a/lib/src/exception/http_exception.dart b/lib/src/exception/http_exception.dart index 18641a8..8bee47e 100644 --- a/lib/src/exception/http_exception.dart +++ b/lib/src/exception/http_exception.dart @@ -1,10 +1,13 @@ +import 'dart:io'; + import '../http/response/response.dart'; import 'base_http_exception.dart'; class HttpResponseException extends BaseHttpResponseException { - HttpResponseException( - {required super.message, - required super.code, - super.responseType = ResponseType.json, - super.errorCode = 'Error'}); + HttpResponseException({ + super.message, + super.code = HttpStatus.found, + super.responseType = ResponseType.json, + super.errorCode = 'Error', + }); } diff --git a/lib/src/exception/redirect_exception.dart b/lib/src/exception/redirect_exception.dart new file mode 100644 index 0000000..aac7b47 --- /dev/null +++ b/lib/src/exception/redirect_exception.dart @@ -0,0 +1,12 @@ +import 'dart:io'; + +import '../http/response/response.dart'; +import 'base_http_exception.dart'; + +class RedirectException extends BaseHttpResponseException { + RedirectException({ + super.message, + super.code = HttpStatus.found, + super.responseType = ResponseType.html, + }); +} diff --git a/lib/src/http/request/request_handler.dart b/lib/src/http/request/request_handler.dart index 277e936..73e4255 100644 --- a/lib/src/http/request/request_handler.dart +++ b/lib/src/http/request/request_handler.dart @@ -97,6 +97,10 @@ class RequestHandler { return Response.redirect(error.message).makeResponse(req.response); } + if (error is RedirectException && isHtml) { + return Response.redirect(error.message).makeResponse(req.response); + } + error .response( isHtml, diff --git a/lib/vania.dart b/lib/vania.dart index 44b47f9..069f937 100644 --- a/lib/vania.dart +++ b/lib/vania.dart @@ -11,6 +11,7 @@ export 'src/http/response/response.dart'; export 'src/exception/base_http_exception.dart'; export 'src/exception/http_exception.dart'; +export 'src/exception/redirect_exception.dart'; export 'src/websocket/websocket_client.dart'; export 'src/websocket/websocket_event.dart';