1
0

Added debug mode

This commit is contained in:
2021-06-03 19:48:04 +02:00
parent 324d28806b
commit 6954f54ca6
2 changed files with 32 additions and 3 deletions

13
index.d.ts vendored
View File

@@ -14,6 +14,13 @@ declare class Router {
* @type {Route[]} * @type {Route[]}
*/ */
protected routes: Route[]; protected routes: Route[];
/**
* Debug Mode
*
* @protected
* @type {boolean}
*/
protected debugMode: boolean = false;
/** /**
* CORS Config * CORS Config
* *
@@ -160,6 +167,12 @@ declare class Router {
* @property {number} maxAge Access-Control-Max-Age (default: `86400`) * @property {number} maxAge Access-Control-Max-Age (default: `86400`)
* @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`) * @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`)
*/ */
/**
* Debug Mode
*
* @param {boolean} state Whether to turn on or off debug mode (default: true)
*/
debug(state: boolean = true): void
/** /**
* Enable CORS support * Enable CORS support
* *

View File

@@ -16,6 +16,14 @@ class Router {
*/ */
this.routes = [] this.routes = []
/**
* Debug Mode
*
* @protected
* @type {boolean}
*/
this.debugMode = false
/** /**
* CORS Config * CORS Config
* *
@@ -193,6 +201,15 @@ class Router {
return this.any(url, handlers) return this.any(url, handlers)
} }
/**
* Debug Mode
*
* @param {boolean} state Whether to turn on or off debug mode (default: true)
*/
debug(state = true) {
this.debugMode = state
}
/** /**
* CORS Config * CORS Config
* *
@@ -302,7 +319,7 @@ class Router {
} }
const route = this.getRoute(request) const route = this.getRoute(request)
if (!route) { if (!route) {
return new Response('', { return new Response(this.debugMode ? 'Route not found!' : '', {
status: 404 status: 404
}) })
} }
@@ -327,8 +344,7 @@ class Router {
} }
await runner(0) await runner(0)
} catch(err) { } catch(err) {
console.error(err) return new Response(this.debugMode ? err.stack : '', {
return new Response('', {
status: 500 status: 500
}) })
} }