Просмотр исходного кода

refactor: replaces Errorf with InvalidArgumentError

e22m4u 3 недель назад
Родитель
Сommit
5aabf1d3ca

+ 88 - 64
dist/cjs/index.cjs

@@ -70,8 +70,8 @@ __export(index_exports, {
 module.exports = __toCommonJS(index_exports);
 
 // src/route.js
-var import_js_format12 = require("@e22m4u/js-format");
 var import_js_debug = require("@e22m4u/js-debug");
+var import_js_format12 = require("@e22m4u/js-format");
 
 // src/hooks/hook-invoker.js
 var import_js_format11 = require("@e22m4u/js-format");
@@ -136,13 +136,13 @@ __name(isPromise, "isPromise");
 var import_js_format = require("@e22m4u/js-format");
 function createError(errorCtor, message, ...args) {
   if (typeof errorCtor !== "function") {
-    throw new import_js_format.Errorf(
+    throw new import_js_format.InvalidArgumentError(
       'The first parameter of "createError" must be a constructor, but %v was given.',
       errorCtor
     );
   }
   if (message != null && typeof message !== "string") {
-    throw new import_js_format.Errorf(
+    throw new import_js_format.InvalidArgumentError(
       'The second parameter of "createError" must be a String, but %v was given.',
       message
     );
@@ -159,7 +159,7 @@ __name(createError, "createError");
 var import_js_format2 = require("@e22m4u/js-format");
 function toCamelCase(input) {
   if (typeof input !== "string") {
-    throw new import_js_format2.Errorf(
+    throw new import_js_format2.InvalidArgumentError(
       'The first parameter of "toCamelCase" must be a String, but %v was given.',
       input
     );
@@ -172,7 +172,7 @@ __name(toCamelCase, "toCamelCase");
 var import_js_format3 = require("@e22m4u/js-format");
 function isResponseSent(response) {
   if (!response || typeof response !== "object" || Array.isArray(response) || typeof response.headersSent !== "boolean") {
-    throw new import_js_format3.Errorf(
+    throw new import_js_format3.InvalidArgumentError(
       'The first parameter of "isResponseSent" must be an instance of ServerResponse, but %v was given.',
       response
     );
@@ -204,7 +204,7 @@ __name(isReadableStream, "isReadableStream");
 var import_js_format4 = require("@e22m4u/js-format");
 function parseContentType(input) {
   if (typeof input !== "string") {
-    throw new import_js_format4.Errorf(
+    throw new import_js_format4.InvalidArgumentError(
       "The first parameter of `parseContentType` must be a String, but %v was given.",
       input
     );
@@ -250,13 +250,13 @@ var CHARACTER_ENCODING_LIST = [
 ];
 function fetchRequestBody(request, bodyBytesLimit = 0) {
   if (!(request instanceof import_http.IncomingMessage)) {
-    throw new import_js_format5.Errorf(
+    throw new import_js_format5.InvalidArgumentError(
       'The first parameter of "fetchRequestBody" must be an IncomingMessage instance, but %v was given.',
       request
     );
   }
   if (typeof bodyBytesLimit !== "number") {
-    throw new import_js_format5.Errorf(
+    throw new import_js_format5.InvalidArgumentError(
       'The parameter "bodyBytesLimit" of "fetchRequestBody" must be a number, but %v was given.',
       bodyBytesLimit
     );
@@ -336,7 +336,7 @@ __name(fetchRequestBody, "fetchRequestBody");
 var import_js_format6 = require("@e22m4u/js-format");
 function parseCookieString(input) {
   if (typeof input !== "string") {
-    throw new import_js_format6.Errorf(
+    throw new import_js_format6.InvalidArgumentError(
       'The first parameter of "parseCookieString" must be a String, but %v was given.',
       input
     );
@@ -361,7 +361,7 @@ var import_js_format8 = require("@e22m4u/js-format");
 var import_js_format7 = require("@e22m4u/js-format");
 function createCookieString(data) {
   if (!data || typeof data !== "object" || Array.isArray(data)) {
-    throw new import_js_format7.Errorf(
+    throw new import_js_format7.InvalidArgumentError(
       'The first parameter of "createCookieString" must be an Object, but %v was given.',
       data
     );
@@ -384,69 +384,69 @@ __name(createCookieString, "createCookieString");
 // src/utils/create-request-mock.js
 function createRequestMock(patch) {
   if (patch != null && typeof patch !== "object" || Array.isArray(patch)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The first parameter of "createRequestMock" must be an Object, but %v was given.',
       patch
     );
   }
   patch = patch || {};
   if (patch.host != null && typeof patch.host !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "host" of "createRequestMock" must be a String, but %v was given.',
       patch.host
     );
   }
   if (patch.method != null && typeof patch.method !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "method" of "createRequestMock" must be a String, but %v was given.',
       patch.method
     );
   }
   if (patch.secure != null && typeof patch.secure !== "boolean") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "secure" of "createRequestMock" must be a Boolean, but %v was given.',
       patch.secure
     );
   }
   if (patch.path != null && typeof patch.path !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "path" of "createRequestMock" must be a String, but %v was given.',
       patch.path
     );
   }
   if (patch.query != null && typeof patch.query !== "object" && typeof patch.query !== "string" || Array.isArray(patch.query)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "query" of "createRequestMock" must be a String or Object, but %v was given.',
       patch.query
     );
   }
   if (patch.cookies != null && typeof patch.cookies !== "string" && typeof patch.cookies !== "object" || Array.isArray(patch.cookies)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "cookies" of "createRequestMock" must be a String or Object, but %v was given.',
       patch.cookies
     );
   }
   if (patch.headers != null && typeof patch.headers !== "object" || Array.isArray(patch.headers)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "headers" of "createRequestMock" must be an Object, but %v was given.',
       patch.headers
     );
   }
   if (patch.stream != null && !isReadableStream(patch.stream)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "stream" of "createRequestMock" must be a Stream, but %v was given.',
       patch.stream
     );
   }
   if (patch.encoding != null) {
     if (typeof patch.encoding !== "string") {
-      throw new import_js_format8.Errorf(
+      throw new import_js_format8.InvalidArgumentError(
         'The parameter "encoding" of "createRequestMock" must be a String, but %v was given.',
         patch.encoding
       );
     }
     if (!CHARACTER_ENCODING_LIST.includes(patch.encoding)) {
-      throw new import_js_format8.Errorf(
+      throw new import_js_format8.InvalidArgumentError(
         "Character encoding %v is not supported.",
         patch.encoding
       );
@@ -454,17 +454,17 @@ function createRequestMock(patch) {
   }
   if (patch.stream) {
     if (patch.secure != null) {
-      throw new import_js_format8.Errorf(
+      throw new import_js_format8.InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the "stream" and "secure" options simultaneously.'
       );
     }
     if (patch.body != null) {
-      throw new import_js_format8.Errorf(
+      throw new import_js_format8.InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the "stream" and "body" options simultaneously.'
       );
     }
     if (patch.encoding != null) {
-      throw new import_js_format8.Errorf(
+      throw new import_js_format8.InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the "stream" and "encoding" options simultaneously.'
       );
     }
@@ -485,7 +485,7 @@ function createRequestMock(patch) {
 __name(createRequestMock, "createRequestMock");
 function createRequestStream(secure, body, encoding) {
   if (encoding != null && typeof encoding !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "encoding" of "createRequestStream" must be a String, but %v was given.',
       encoding
     );
@@ -511,13 +511,13 @@ function createRequestStream(secure, body, encoding) {
 __name(createRequestStream, "createRequestStream");
 function createRequestUrl(path, query) {
   if (typeof path !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "path" of "createRequestUrl" must be a String, but %v was given.',
       path
     );
   }
   if (query != null && typeof query !== "string" && typeof query !== "object" || Array.isArray(query)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "query" of "createRequestUrl" must be a String or Object, but %v was given.',
       query
     );
@@ -536,34 +536,34 @@ function createRequestUrl(path, query) {
 __name(createRequestUrl, "createRequestUrl");
 function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
   if (host != null && typeof host !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "host" of "createRequestHeaders" a non-empty String, but %v was given.',
       host
     );
   }
   host = host || "localhost";
   if (secure != null && typeof secure !== "boolean") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "secure" of "createRequestHeaders" must be a String, but %v was given.',
       secure
     );
   }
   secure = Boolean(secure);
   if (cookies != null && typeof cookies !== "object" && typeof cookies !== "string" || Array.isArray(cookies)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "cookies" of "createRequestHeaders" must be a String or Object, but %v was given.',
       cookies
     );
   }
   if (headers != null && typeof headers !== "object" || Array.isArray(headers)) {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "headers" of "createRequestHeaders" must be an Object, but %v was given.',
       headers
     );
   }
   headers = headers || {};
   if (encoding != null && typeof encoding !== "string") {
-    throw new import_js_format8.Errorf(
+    throw new import_js_format8.InvalidArgumentError(
       'The parameter "encoding" of "createRequestHeaders" must be a String, but %v was given.',
       encoding
     );
@@ -719,7 +719,7 @@ __name(patchBody, "patchBody");
 var import_js_format9 = require("@e22m4u/js-format");
 function getRequestPathname(request) {
   if (!request || typeof request !== "object" || Array.isArray(request) || typeof request.url !== "string") {
-    throw new import_js_format9.Errorf(
+    throw new import_js_format9.InvalidArgumentError(
       'The first parameter of "getRequestPathname" must be an instance of IncomingMessage, but %v was given.',
       request
     );
@@ -751,13 +751,19 @@ var _HookRegistry = class _HookRegistry {
    */
   addHook(type, hook) {
     if (!type || typeof type !== "string") {
-      throw new import_js_format10.Errorf("The hook type is required, but %v was given.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type is required, but %v was given.",
+        type
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new import_js_format10.Errorf("The hook type %v is not supported.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type %v is not supported.",
+        type
+      );
     }
     if (!hook || typeof hook !== "function") {
-      throw new import_js_format10.Errorf(
+      throw new import_js_format10.InvalidArgumentError(
         "The hook %v must be a Function, but %v was given.",
         type,
         hook
@@ -777,13 +783,19 @@ var _HookRegistry = class _HookRegistry {
    */
   hasHook(type, hook) {
     if (!type || typeof type !== "string") {
-      throw new import_js_format10.Errorf("The hook type is required, but %v was given.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type is required, but %v was given.",
+        type
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new import_js_format10.Errorf("The hook type %v is not supported.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type %v is not supported.",
+        type
+      );
     }
     if (!hook || typeof hook !== "function") {
-      throw new import_js_format10.Errorf(
+      throw new import_js_format10.InvalidArgumentError(
         "The hook %v must be a Function, but %v was given.",
         type,
         hook
@@ -800,10 +812,16 @@ var _HookRegistry = class _HookRegistry {
    */
   getHooks(type) {
     if (!type || typeof type !== "string") {
-      throw new import_js_format10.Errorf("The hook type is required, but %v was given.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type is required, but %v was given.",
+        type
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new import_js_format10.Errorf("The hook type %v is not supported.", type);
+      throw new import_js_format10.InvalidArgumentError(
+        "The hook type %v is not supported.",
+        type
+      );
     }
     return this._hooks.get(type) || [];
   }
@@ -824,22 +842,25 @@ var _HookInvoker = class _HookInvoker extends DebuggableService {
    */
   invokeAndContinueUntilValueReceived(route, hookType, response, ...args) {
     if (!route || !(route instanceof Route)) {
-      throw new import_js_format11.Errorf(
+      throw new import_js_format11.InvalidArgumentError(
         'The parameter "route" of the HookInvoker.invokeAndContinueUntilValueReceived must be a Route instance, but %v was given.',
         route
       );
     }
     if (!hookType || typeof hookType !== "string") {
-      throw new import_js_format11.Errorf(
+      throw new import_js_format11.InvalidArgumentError(
         'The parameter "hookType" of the HookInvoker.invokeAndContinueUntilValueReceived must be a non-empty String, but %v was given.',
         hookType
       );
     }
     if (!Object.values(RouterHookType).includes(hookType)) {
-      throw new import_js_format11.Errorf("The hook type %v is not supported.", hookType);
+      throw new import_js_format11.InvalidArgumentError(
+        "The hook type %v is not supported.",
+        hookType
+      );
     }
     if (!response || typeof response !== "object" || Array.isArray(response) || typeof response.headersSent !== "boolean") {
-      throw new import_js_format11.Errorf(
+      throw new import_js_format11.InvalidArgumentError(
         'The parameter "response" of the HookInvoker.invokeAndContinueUntilValueReceived must be a ServerResponse instance, but %v was given.',
         response
       );
@@ -984,34 +1005,34 @@ var _Route = class _Route extends import_js_debug.Debuggable {
       noInstantiationMessage: true
     });
     if (!routeDef || typeof routeDef !== "object" || Array.isArray(routeDef)) {
-      throw new import_js_format12.Errorf(
+      throw new import_js_format12.InvalidArgumentError(
         "The first parameter of Route.constructor must be an Object, but %v was given.",
         routeDef
       );
     }
     if (!routeDef.method || typeof routeDef.method !== "string") {
-      throw new import_js_format12.Errorf(
+      throw new import_js_format12.InvalidArgumentError(
         'The option "method" of the Route must be a non-empty String, but %v was given.',
         routeDef.method
       );
     }
     this._method = routeDef.method.toUpperCase();
     if (typeof routeDef.path !== "string") {
-      throw new import_js_format12.Errorf(
+      throw new import_js_format12.InvalidArgumentError(
         'The option "path" of the Route must be a String, but %v was given.',
         routeDef.path
       );
     }
     this._path = routeDef.path;
     if (typeof routeDef.handler !== "function") {
-      throw new import_js_format12.Errorf(
+      throw new import_js_format12.InvalidArgumentError(
         'The option "handler" of the Route must be a Function, but %v was given.',
         routeDef.handler
       );
     }
     if (routeDef.meta != null) {
       if (typeof routeDef.meta !== "object" || Array.isArray(routeDef.meta)) {
-        throw new import_js_format12.Errorf(
+        throw new import_js_format12.InvalidArgumentError(
           'The option "meta" of the Route must be a plain Object, but %v was given.',
           routeDef.meta
         );
@@ -1055,7 +1076,6 @@ var Route = _Route;
 
 // src/parsers/body-parser.js
 var import_http_errors2 = __toESM(require("http-errors"), 1);
-var import_js_format14 = require("@e22m4u/js-format");
 
 // src/router-options.js
 var import_js_format13 = require("@e22m4u/js-format");
@@ -1084,7 +1104,7 @@ var _RouterOptions = class _RouterOptions extends DebuggableService {
    */
   setRequestBodyBytesLimit(input) {
     if (typeof input !== "number" || input < 0) {
-      throw new import_js_format13.Errorf(
+      throw new import_js_format13.InvalidArgumentError(
         'The option "requestBodyBytesLimit" must be a positive Number or 0, but %v was given.',
         input
       );
@@ -1097,6 +1117,7 @@ __name(_RouterOptions, "RouterOptions");
 var RouterOptions = _RouterOptions;
 
 // src/parsers/body-parser.js
+var import_js_format14 = require("@e22m4u/js-format");
 var METHODS_WITH_BODY = ["POST", "PUT", "PATCH", "DELETE"];
 var UNPARSABLE_MEDIA_TYPES = ["multipart/form-data"];
 var _BodyParser = class _BodyParser extends DebuggableService {
@@ -1118,13 +1139,13 @@ var _BodyParser = class _BodyParser extends DebuggableService {
    */
   defineParser(mediaType, parser) {
     if (!mediaType || typeof mediaType !== "string") {
-      throw new import_js_format14.Errorf(
+      throw new import_js_format14.InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.defineParser must be a non-empty String, but %v was given.',
         mediaType
       );
     }
     if (!parser || typeof parser !== "function") {
-      throw new import_js_format14.Errorf(
+      throw new import_js_format14.InvalidArgumentError(
         'The parameter "parser" of BodyParser.defineParser must be a Function, but %v was given.',
         parser
       );
@@ -1140,7 +1161,7 @@ var _BodyParser = class _BodyParser extends DebuggableService {
    */
   hasParser(mediaType) {
     if (!mediaType || typeof mediaType !== "string") {
-      throw new import_js_format14.Errorf(
+      throw new import_js_format14.InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.hasParser must be a non-empty String, but %v was given.',
         mediaType
       );
@@ -1155,14 +1176,17 @@ var _BodyParser = class _BodyParser extends DebuggableService {
    */
   deleteParser(mediaType) {
     if (!mediaType || typeof mediaType !== "string") {
-      throw new import_js_format14.Errorf(
+      throw new import_js_format14.InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.deleteParser must be a non-empty String, but %v was given.',
         mediaType
       );
     }
     const parser = this._parsers[mediaType];
     if (!parser) {
-      throw new import_js_format14.Errorf("The parser of %v is not found.", mediaType);
+      throw new import_js_format14.InvalidArgumentError(
+        "The parser of %v is not found.",
+        mediaType
+      );
     }
     delete this._parsers[mediaType];
     return this;
@@ -1307,7 +1331,7 @@ var _RequestParser = class _RequestParser extends DebuggableService {
    */
   parse(request) {
     if (!(request instanceof import_http3.IncomingMessage)) {
-      throw new import_js_format15.Errorf(
+      throw new import_js_format15.InvalidArgumentError(
         "The first parameter of RequestParser.parse must be an instance of IncomingMessage, but %v was given.",
         request
       );
@@ -1340,9 +1364,9 @@ __name(_RequestParser, "RequestParser");
 var RequestParser = _RequestParser;
 
 // src/route-registry.js
-var import_js_format16 = require("@e22m4u/js-format");
 var import_js_path_trie = require("@e22m4u/js-path-trie");
 var import_js_service2 = require("@e22m4u/js-service");
+var import_js_format16 = require("@e22m4u/js-format");
 var _RouteRegistry = class _RouteRegistry extends DebuggableService {
   /**
    * Constructor.
@@ -1362,7 +1386,7 @@ var _RouteRegistry = class _RouteRegistry extends DebuggableService {
   defineRoute(routeDef) {
     const debug = this.getDebuggerFor(this.defineRoute);
     if (!routeDef || typeof routeDef !== "object" || Array.isArray(routeDef)) {
-      throw new import_js_format16.Errorf(
+      throw new import_js_format16.InvalidArgumentError(
         "The route definition must be an Object, but %v was given.",
         routeDef
       );
@@ -1574,28 +1598,28 @@ var _RequestContext = class _RequestContext {
    */
   constructor(container, request, response, route) {
     if (!(0, import_js_service3.isServiceContainer)(container)) {
-      throw new import_js_format17.Errorf(
+      throw new import_js_format17.InvalidArgumentError(
         'The parameter "container" of RequestContext.constructor must be an instance of ServiceContainer, but %v was given.',
         container
       );
     }
     this._container = container;
     if (!request || typeof request !== "object" || Array.isArray(request) || !isReadableStream(request)) {
-      throw new import_js_format17.Errorf(
+      throw new import_js_format17.InvalidArgumentError(
         'The parameter "request" of RequestContext.constructor must be an instance of IncomingMessage, but %v was given.',
         request
       );
     }
     this._request = request;
     if (!response || typeof response !== "object" || Array.isArray(response) || !isWritableStream(response)) {
-      throw new import_js_format17.Errorf(
+      throw new import_js_format17.InvalidArgumentError(
         'The parameter "response" of RequestContext.constructor must be an instance of ServerResponse, but %v was given.',
         response
       );
     }
     this._response = response;
     if (!(route instanceof Route)) {
-      throw new import_js_format17.Errorf(
+      throw new import_js_format17.InvalidArgumentError(
         'The parameter "route" of RequestContext.constructor must be an instance of Route, but %v was given.',
         route
       );

+ 8 - 5
src/hooks/hook-invoker.js

@@ -1,5 +1,5 @@
 import {Route} from '../route.js';
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {DebuggableService} from '../debuggable-service.js';
 import {isPromise, isResponseSent} from '../utils/index.js';
 import {HookRegistry, RouterHookType} from './hook-registry.js';
@@ -19,7 +19,7 @@ export class HookInvoker extends DebuggableService {
    */
   invokeAndContinueUntilValueReceived(route, hookType, response, ...args) {
     if (!route || !(route instanceof Route)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "route" of ' +
           'the HookInvoker.invokeAndContinueUntilValueReceived ' +
           'must be a Route instance, but %v was given.',
@@ -27,7 +27,7 @@ export class HookInvoker extends DebuggableService {
       );
     }
     if (!hookType || typeof hookType !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "hookType" of ' +
           'the HookInvoker.invokeAndContinueUntilValueReceived ' +
           'must be a non-empty String, but %v was given.',
@@ -35,7 +35,10 @@ export class HookInvoker extends DebuggableService {
       );
     }
     if (!Object.values(RouterHookType).includes(hookType)) {
-      throw new Errorf('The hook type %v is not supported.', hookType);
+      throw new InvalidArgumentError(
+        'The hook type %v is not supported.',
+        hookType,
+      );
     }
     if (
       !response ||
@@ -43,7 +46,7 @@ export class HookInvoker extends DebuggableService {
       Array.isArray(response) ||
       typeof response.headersSent !== 'boolean'
     ) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "response" of ' +
           'the HookInvoker.invokeAndContinueUntilValueReceived ' +
           'must be a ServerResponse instance, but %v was given.',

+ 27 - 9
src/hooks/hook-registry.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Hook type.
@@ -34,13 +34,19 @@ export class HookRegistry {
    */
   addHook(type, hook) {
     if (!type || typeof type !== 'string') {
-      throw new Errorf('The hook type is required, but %v was given.', type);
+      throw new InvalidArgumentError(
+        'The hook type is required, but %v was given.',
+        type,
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new Errorf('The hook type %v is not supported.', type);
+      throw new InvalidArgumentError(
+        'The hook type %v is not supported.',
+        type,
+      );
     }
     if (!hook || typeof hook !== 'function') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The hook %v must be a Function, but %v was given.',
         type,
         hook,
@@ -61,13 +67,19 @@ export class HookRegistry {
    */
   hasHook(type, hook) {
     if (!type || typeof type !== 'string') {
-      throw new Errorf('The hook type is required, but %v was given.', type);
+      throw new InvalidArgumentError(
+        'The hook type is required, but %v was given.',
+        type,
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new Errorf('The hook type %v is not supported.', type);
+      throw new InvalidArgumentError(
+        'The hook type %v is not supported.',
+        type,
+      );
     }
     if (!hook || typeof hook !== 'function') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The hook %v must be a Function, but %v was given.',
         type,
         hook,
@@ -85,10 +97,16 @@ export class HookRegistry {
    */
   getHooks(type) {
     if (!type || typeof type !== 'string') {
-      throw new Errorf('The hook type is required, but %v was given.', type);
+      throw new InvalidArgumentError(
+        'The hook type is required, but %v was given.',
+        type,
+      );
     }
     if (!Object.values(RouterHookType).includes(type)) {
-      throw new Errorf('The hook type %v is not supported.', type);
+      throw new InvalidArgumentError(
+        'The hook type %v is not supported.',
+        type,
+      );
     }
     return this._hooks.get(type) || [];
   }

+ 9 - 6
src/parsers/body-parser.js

@@ -1,6 +1,6 @@
 import HttpErrors from 'http-errors';
-import {Errorf} from '@e22m4u/js-format';
 import {RouterOptions} from '../router-options.js';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {DebuggableService} from '../debuggable-service.js';
 
 import {
@@ -46,14 +46,14 @@ export class BodyParser extends DebuggableService {
    */
   defineParser(mediaType, parser) {
     if (!mediaType || typeof mediaType !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.defineParser ' +
           'must be a non-empty String, but %v was given.',
         mediaType,
       );
     }
     if (!parser || typeof parser !== 'function') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "parser" of BodyParser.defineParser ' +
           'must be a Function, but %v was given.',
         parser,
@@ -71,7 +71,7 @@ export class BodyParser extends DebuggableService {
    */
   hasParser(mediaType) {
     if (!mediaType || typeof mediaType !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.hasParser ' +
           'must be a non-empty String, but %v was given.',
         mediaType,
@@ -88,7 +88,7 @@ export class BodyParser extends DebuggableService {
    */
   deleteParser(mediaType) {
     if (!mediaType || typeof mediaType !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "mediaType" of BodyParser.deleteParser ' +
           'must be a non-empty String, but %v was given.',
         mediaType,
@@ -96,7 +96,10 @@ export class BodyParser extends DebuggableService {
     }
     const parser = this._parsers[mediaType];
     if (!parser) {
-      throw new Errorf('The parser of %v is not found.', mediaType);
+      throw new InvalidArgumentError(
+        'The parser of %v is not found.',
+        mediaType,
+      );
     }
     delete this._parsers[mediaType];
     return this;

+ 2 - 2
src/parsers/request-parser.js

@@ -1,9 +1,9 @@
 import {IncomingMessage} from 'http';
-import {Errorf} from '@e22m4u/js-format';
 import {isPromise} from '../utils/index.js';
 import {BodyParser} from './body-parser.js';
 import {QueryParser} from './query-parser.js';
 import {CookiesParser} from './cookies-parser.js';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {DebuggableService} from '../debuggable-service.js';
 
 /**
@@ -18,7 +18,7 @@ export class RequestParser extends DebuggableService {
    */
   parse(request) {
     if (!(request instanceof IncomingMessage)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The first parameter of RequestParser.parse must be ' +
           'an instance of IncomingMessage, but %v was given.',
         request,

+ 5 - 5
src/request-context.js

@@ -1,5 +1,5 @@
 import {Route} from './route.js';
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {ServiceContainer, isServiceContainer} from '@e22m4u/js-service';
 
 import {
@@ -176,7 +176,7 @@ export class RequestContext {
    */
   constructor(container, request, response, route) {
     if (!isServiceContainer(container)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "container" of RequestContext.constructor ' +
           'must be an instance of ServiceContainer, but %v was given.',
         container,
@@ -189,7 +189,7 @@ export class RequestContext {
       Array.isArray(request) ||
       !isReadableStream(request)
     ) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "request" of RequestContext.constructor ' +
           'must be an instance of IncomingMessage, but %v was given.',
         request,
@@ -202,7 +202,7 @@ export class RequestContext {
       Array.isArray(response) ||
       !isWritableStream(response)
     ) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "response" of RequestContext.constructor ' +
           'must be an instance of ServerResponse, but %v was given.',
         response,
@@ -210,7 +210,7 @@ export class RequestContext {
     }
     this._response = response;
     if (!(route instanceof Route)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "route" of RequestContext.constructor ' +
           'must be an instance of Route, but %v was given.',
         route,

+ 2 - 2
src/route-registry.js

@@ -1,7 +1,7 @@
 import {Route} from './route.js';
-import {Errorf} from '@e22m4u/js-format';
 import {PathTrie} from '@e22m4u/js-path-trie';
 import {ServiceContainer} from '@e22m4u/js-service';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {DebuggableService} from './debuggable-service.js';
 
 /**
@@ -34,7 +34,7 @@ export class RouteRegistry extends DebuggableService {
   defineRoute(routeDef) {
     const debug = this.getDebuggerFor(this.defineRoute);
     if (!routeDef || typeof routeDef !== 'object' || Array.isArray(routeDef)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The route definition must be an Object, but %v was given.',
         routeDef,
       );

+ 6 - 6
src/route.js

@@ -1,5 +1,5 @@
-import {Errorf} from '@e22m4u/js-format';
 import {Debuggable} from '@e22m4u/js-debug';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {HookRegistry, RouterHookType} from './hooks/index.js';
 import {MODULE_DEBUG_NAMESPACE} from './debuggable-service.js';
 import {cloneDeep, getRequestPathname} from './utils/index.js';
@@ -138,14 +138,14 @@ export class Route extends Debuggable {
       noInstantiationMessage: true,
     });
     if (!routeDef || typeof routeDef !== 'object' || Array.isArray(routeDef)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The first parameter of Route.constructor ' +
           'must be an Object, but %v was given.',
         routeDef,
       );
     }
     if (!routeDef.method || typeof routeDef.method !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The option "method" of the Route must be ' +
           'a non-empty String, but %v was given.',
         routeDef.method,
@@ -153,7 +153,7 @@ export class Route extends Debuggable {
     }
     this._method = routeDef.method.toUpperCase();
     if (typeof routeDef.path !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The option "path" of the Route must be ' +
           'a String, but %v was given.',
         routeDef.path,
@@ -161,7 +161,7 @@ export class Route extends Debuggable {
     }
     this._path = routeDef.path;
     if (typeof routeDef.handler !== 'function') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The option "handler" of the Route must be ' +
           'a Function, but %v was given.',
         routeDef.handler,
@@ -169,7 +169,7 @@ export class Route extends Debuggable {
     }
     if (routeDef.meta != null) {
       if (typeof routeDef.meta !== 'object' || Array.isArray(routeDef.meta)) {
-        throw new Errorf(
+        throw new InvalidArgumentError(
           'The option "meta" of the Route must be ' +
             'a plain Object, but %v was given.',
           routeDef.meta,

+ 2 - 2
src/router-options.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {DebuggableService} from './debuggable-service.js';
 
 /**
@@ -30,7 +30,7 @@ export class RouterOptions extends DebuggableService {
    */
   setRequestBodyBytesLimit(input) {
     if (typeof input !== 'number' || input < 0) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The option "requestBodyBytesLimit" must be ' +
           'a positive Number or 0, but %v was given.',
         input,

+ 2 - 2
src/utils/create-cookie-string.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Create cookie string.
@@ -8,7 +8,7 @@ import {Errorf} from '@e22m4u/js-format';
  */
 export function createCookieString(data) {
   if (!data || typeof data !== 'object' || Array.isArray(data)) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "createCookieString" must be ' +
         'an Object, but %v was given.',
       data,

+ 3 - 3
src/utils/create-error.js

@@ -1,4 +1,4 @@
-import {format, Errorf} from '@e22m4u/js-format';
+import {format, InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Create error.
@@ -10,14 +10,14 @@ import {format, Errorf} from '@e22m4u/js-format';
  */
 export function createError(errorCtor, message, ...args) {
   if (typeof errorCtor !== 'function') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "createError" must be ' +
         'a constructor, but %v was given.',
       errorCtor,
     );
   }
   if (message != null && typeof message !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The second parameter of "createError" must be ' +
         'a String, but %v was given.',
       message,

+ 23 - 23
src/utils/create-request-mock.js

@@ -2,7 +2,7 @@ import {Socket} from 'net';
 import {TLSSocket} from 'tls';
 import {IncomingMessage} from 'http';
 import queryString from 'querystring';
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {isReadableStream} from './is-readable-stream.js';
 import {createCookieString} from './create-cookie-string.js';
 import {CHARACTER_ENCODING_LIST} from './fetch-request-body.js';
@@ -30,7 +30,7 @@ import {CHARACTER_ENCODING_LIST} from './fetch-request-body.js';
  */
 export function createRequestMock(patch) {
   if ((patch != null && typeof patch !== 'object') || Array.isArray(patch)) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "createRequestMock" ' +
         'must be an Object, but %v was given.',
       patch,
@@ -38,28 +38,28 @@ export function createRequestMock(patch) {
   }
   patch = patch || {};
   if (patch.host != null && typeof patch.host !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "host" of "createRequestMock" ' +
         'must be a String, but %v was given.',
       patch.host,
     );
   }
   if (patch.method != null && typeof patch.method !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "method" of "createRequestMock" ' +
         'must be a String, but %v was given.',
       patch.method,
     );
   }
   if (patch.secure != null && typeof patch.secure !== 'boolean') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "secure" of "createRequestMock" ' +
         'must be a Boolean, but %v was given.',
       patch.secure,
     );
   }
   if (patch.path != null && typeof patch.path !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "path" of "createRequestMock" ' +
         'must be a String, but %v was given.',
       patch.path,
@@ -71,7 +71,7 @@ export function createRequestMock(patch) {
       typeof patch.query !== 'string') ||
     Array.isArray(patch.query)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "query" of "createRequestMock" ' +
         'must be a String or Object, but %v was given.',
       patch.query,
@@ -83,7 +83,7 @@ export function createRequestMock(patch) {
       typeof patch.cookies !== 'object') ||
     Array.isArray(patch.cookies)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "cookies" of "createRequestMock" ' +
         'must be a String or Object, but %v was given.',
       patch.cookies,
@@ -93,14 +93,14 @@ export function createRequestMock(patch) {
     (patch.headers != null && typeof patch.headers !== 'object') ||
     Array.isArray(patch.headers)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "headers" of "createRequestMock" ' +
         'must be an Object, but %v was given.',
       patch.headers,
     );
   }
   if (patch.stream != null && !isReadableStream(patch.stream)) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "stream" of "createRequestMock" ' +
         'must be a Stream, but %v was given.',
       patch.stream,
@@ -108,14 +108,14 @@ export function createRequestMock(patch) {
   }
   if (patch.encoding != null) {
     if (typeof patch.encoding !== 'string') {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The parameter "encoding" of "createRequestMock" ' +
           'must be a String, but %v was given.',
         patch.encoding,
       );
     }
     if (!CHARACTER_ENCODING_LIST.includes(patch.encoding)) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'Character encoding %v is not supported.',
         patch.encoding,
       );
@@ -125,19 +125,19 @@ export function createRequestMock(patch) {
   // проверка на несовместимые опции
   if (patch.stream) {
     if (patch.secure != null) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the ' +
           '"stream" and "secure" options simultaneously.',
       );
     }
     if (patch.body != null) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the ' +
           '"stream" and "body" options simultaneously.',
       );
     }
     if (patch.encoding != null) {
-      throw new Errorf(
+      throw new InvalidArgumentError(
         'The "createRequestMock" does not allow specifying the ' +
           '"stream" and "encoding" options simultaneously.',
       );
@@ -172,7 +172,7 @@ export function createRequestMock(patch) {
  */
 function createRequestStream(secure, body, encoding) {
   if (encoding != null && typeof encoding !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "encoding" of "createRequestStream" ' +
         'must be a String, but %v was given.',
       encoding,
@@ -212,7 +212,7 @@ function createRequestStream(secure, body, encoding) {
  */
 function createRequestUrl(path, query) {
   if (typeof path !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "path" of "createRequestUrl" ' +
         'must be a String, but %v was given.',
       path,
@@ -222,7 +222,7 @@ function createRequestUrl(path, query) {
     (query != null && typeof query !== 'string' && typeof query !== 'object') ||
     Array.isArray(query)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "query" of "createRequestUrl" ' +
         'must be a String or Object, but %v was given.',
       query,
@@ -253,7 +253,7 @@ function createRequestUrl(path, query) {
  */
 function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
   if (host != null && typeof host !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "host" of "createRequestHeaders" ' +
         'a non-empty String, but %v was given.',
       host,
@@ -261,7 +261,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
   }
   host = host || 'localhost';
   if (secure != null && typeof secure !== 'boolean') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "secure" of "createRequestHeaders" ' +
         'must be a String, but %v was given.',
       secure,
@@ -274,7 +274,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
       typeof cookies !== 'string') ||
     Array.isArray(cookies)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "cookies" of "createRequestHeaders" ' +
         'must be a String or Object, but %v was given.',
       cookies,
@@ -284,7 +284,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
     (headers != null && typeof headers !== 'object') ||
     Array.isArray(headers)
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "headers" of "createRequestHeaders" ' +
         'must be an Object, but %v was given.',
       headers,
@@ -292,7 +292,7 @@ function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
   }
   headers = headers || {};
   if (encoding != null && typeof encoding !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "encoding" of "createRequestHeaders" ' +
         'must be a String, but %v was given.',
       encoding,

+ 3 - 3
src/utils/fetch-request-body.js

@@ -1,7 +1,7 @@
 import HttpErrors from 'http-errors';
 import {IncomingMessage} from 'http';
-import {Errorf} from '@e22m4u/js-format';
 import {createError} from './create-error.js';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 import {parseContentType} from './parse-content-type.js';
 
 /**
@@ -27,14 +27,14 @@ export const CHARACTER_ENCODING_LIST = [
  */
 export function fetchRequestBody(request, bodyBytesLimit = 0) {
   if (!(request instanceof IncomingMessage)) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "fetchRequestBody" must be ' +
         'an IncomingMessage instance, but %v was given.',
       request,
     );
   }
   if (typeof bodyBytesLimit !== 'number') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The parameter "bodyBytesLimit" of "fetchRequestBody" ' +
         'must be a number, but %v was given.',
       bodyBytesLimit,

+ 2 - 2
src/utils/get-request-pathname.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Get request pathname.
@@ -13,7 +13,7 @@ export function getRequestPathname(request) {
     Array.isArray(request) ||
     typeof request.url !== 'string'
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "getRequestPathname" must be ' +
         'an instance of IncomingMessage, but %v was given.',
       request,

+ 2 - 2
src/utils/is-response-sent.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Is response sent.
@@ -13,7 +13,7 @@ export function isResponseSent(response) {
     Array.isArray(response) ||
     typeof response.headersSent !== 'boolean'
   ) {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "isResponseSent" must be ' +
         'an instance of ServerResponse, but %v was given.',
       response,

+ 2 - 2
src/utils/parse-content-type.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Parse content type.
@@ -12,7 +12,7 @@ import {Errorf} from '@e22m4u/js-format';
  */
 export function parseContentType(input) {
   if (typeof input !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of `parseContentType` ' +
         'must be a String, but %v was given.',
       input,

+ 2 - 2
src/utils/parse-cookie-string.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * Parse cookie string.
@@ -14,7 +14,7 @@ import {Errorf} from '@e22m4u/js-format';
  */
 export function parseCookieString(input) {
   if (typeof input !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "parseCookieString" must be a String, ' +
         'but %v was given.',
       input,

+ 2 - 2
src/utils/to-camel-case.js

@@ -1,4 +1,4 @@
-import {Errorf} from '@e22m4u/js-format';
+import {InvalidArgumentError} from '@e22m4u/js-format';
 
 /**
  * To camel case.
@@ -8,7 +8,7 @@ import {Errorf} from '@e22m4u/js-format';
  */
 export function toCamelCase(input) {
   if (typeof input !== 'string') {
-    throw new Errorf(
+    throw new InvalidArgumentError(
       'The first parameter of "toCamelCase" ' +
         'must be a String, but %v was given.',
       input,