diff --git a/Sources/Website/Middlewares/ErrorMiddleware.swift b/Sources/Website/Middlewares/ErrorMiddleware.swift new file mode 100644 index 0000000..aceb0ca --- /dev/null +++ b/Sources/Website/Middlewares/ErrorMiddleware.swift @@ -0,0 +1,18 @@ +import Vapor + +struct ErrorMiddleware: AsyncMiddleware { + + public func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response { + + do { + return try await next.respond(to: request) + + } catch { + + request.logger.report(error: error) + + return try await request.htmlkit.render(ErrorPage.ErrorView(message: error.localizedDescription)) + .encodeResponse(for: request) + } + } +} diff --git a/Sources/Website/Setup.swift b/Sources/Website/Setup.swift index b034ce2..823a7ad 100644 --- a/Sources/Website/Setup.swift +++ b/Sources/Website/Setup.swift @@ -21,6 +21,8 @@ struct Setup { application.passwords.use(.bcrypt) application.sessions.use(.fluent(.mysql)) + application.middleware = .init() + application.middleware.use(ErrorMiddleware()) application.middleware.use(FileMiddleware(publicDirectory: application.directory.publicDirectory)) application.middleware.use(application.sessions.middleware) diff --git a/Sources/Website/Views/Shared/ErrorPage.swift b/Sources/Website/Views/Shared/ErrorPage.swift new file mode 100644 index 0000000..876ff76 --- /dev/null +++ b/Sources/Website/Views/Shared/ErrorPage.swift @@ -0,0 +1,26 @@ +import HTMLKit +import HTMLKitComponents + +enum ErrorPage { + + struct ErrorView: View { + + var message: String + + var body: Content { + AreaViewContainer { + Section { + VStack(spacing: .small) { + Text { + "Ooops!" + } + .textStyle(.headline) + Text { + "Something went wrong (\(message)). Sorry for the inconvenience!" + } + } + } + } + } + } +}