@@ -59,7 +59,17 @@ const { defaultInitOptions } = getSecuredInitialConfig
59
59
const {
60
60
FST_ERR_ASYNC_CONSTRAINT ,
61
61
FST_ERR_BAD_URL ,
62
- FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE
62
+ FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE ,
63
+ FST_ERR_OPTIONS_NOT_OBJ ,
64
+ FST_ERR_QSP_NOT_FN ,
65
+ FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN ,
66
+ FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ ,
67
+ FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR ,
68
+ FST_ERR_VERSION_CONSTRAINT_NOT_STR ,
69
+ FST_ERR_INSTANCE_ALREADY_LISTENING ,
70
+ FST_ERR_REOPENED_CLOSE_SERVER ,
71
+ FST_ERR_ROUTE_REWRITE_NOT_STR ,
72
+ FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN
63
73
} = errorCodes
64
74
65
75
const { buildErrorHandler } = require ( './lib/error-handler.js' )
@@ -90,15 +100,15 @@ function fastify (options) {
90
100
options = options || { }
91
101
92
102
if ( typeof options !== 'object' ) {
93
- throw new TypeError ( 'Options must be an object' )
103
+ throw new FST_ERR_OPTIONS_NOT_OBJ ( )
94
104
}
95
105
96
106
if ( options . querystringParser && typeof options . querystringParser !== 'function' ) {
97
- throw new Error ( `querystringParser option should be a function, instead got ' ${ typeof options . querystringParser } '` )
107
+ throw new FST_ERR_QSP_NOT_FN ( typeof options . querystringParser )
98
108
}
99
109
100
110
if ( options . schemaController && options . schemaController . bucket && typeof options . schemaController . bucket !== 'function' ) {
101
- throw new Error ( `schemaController.bucket option should be a function, instead got ' ${ typeof options . schemaController . bucket } '` )
111
+ throw new FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN ( typeof options . schemaController . bucket )
102
112
}
103
113
104
114
validateBodyLimitOption ( options . bodyLimit )
@@ -117,10 +127,10 @@ function fastify (options) {
117
127
118
128
// Ajv options
119
129
if ( ! ajvOptions . customOptions || Object . prototype . toString . call ( ajvOptions . customOptions ) !== '[object Object]' ) {
120
- throw new Error ( `ajv.customOptions option should be an object, instead got ' ${ typeof ajvOptions . customOptions } '` )
130
+ throw new FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ ( typeof ajvOptions . customOptions )
121
131
}
122
132
if ( ! ajvOptions . plugins || ! Array . isArray ( ajvOptions . plugins ) ) {
123
- throw new Error ( `ajv.plugins option should be an array, instead got ' ${ typeof ajvOptions . plugins } '` )
133
+ throw new FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR ( typeof ajvOptions . plugins )
124
134
}
125
135
126
136
// Instance Fastify components
@@ -156,7 +166,7 @@ function fastify (options) {
156
166
deriveConstraint : options . versioning . deriveVersion ,
157
167
validate ( value ) {
158
168
if ( typeof value !== 'string' ) {
159
- throw new Error ( 'Version constraint should be a string.' )
169
+ throw new FST_ERR_VERSION_CONSTRAINT_NOT_STR ( )
160
170
}
161
171
}
162
172
}
@@ -463,7 +473,7 @@ function fastify (options) {
463
473
return fastify
464
474
465
475
function throwIfAlreadyStarted ( msg ) {
466
- if ( fastify [ kState ] . started ) throw new Error ( msg )
476
+ if ( fastify [ kState ] . started ) throw new FST_ERR_INSTANCE_ALREADY_LISTENING ( msg )
467
477
}
468
478
469
479
// HTTP injection handling
@@ -479,7 +489,7 @@ function fastify (options) {
479
489
if ( fastify [ kState ] . started ) {
480
490
if ( fastify [ kState ] . closing ) {
481
491
// Force to return an error
482
- const error = new Error ( 'Server is closed' )
492
+ const error = new FST_ERR_REOPENED_CLOSE_SERVER ( )
483
493
if ( cb ) {
484
494
cb ( error )
485
495
return
@@ -564,7 +574,7 @@ function fastify (options) {
564
574
565
575
// wrapper that we expose to the user for hooks handling
566
576
function addHook ( name , fn ) {
567
- throwIfAlreadyStarted ( 'Cannot call "addHook" when fastify instance is already started !' )
577
+ throwIfAlreadyStarted ( 'Cannot call "addHook"!' )
568
578
569
579
if ( fn == null ) {
570
580
throw new errorCodes . FST_ERR_HOOK_INVALID_HANDLER ( name , fn )
@@ -607,7 +617,7 @@ function fastify (options) {
607
617
608
618
// wrapper that we expose to the user for schemas handling
609
619
function addSchema ( schema ) {
610
- throwIfAlreadyStarted ( 'Cannot call "addSchema" when fastify instance is already started !' )
620
+ throwIfAlreadyStarted ( 'Cannot call "addSchema"!' )
611
621
this [ kSchemaController ] . add ( schema )
612
622
this [ kChildren ] . forEach ( child => child . addSchema ( schema ) )
613
623
return this
@@ -697,33 +707,33 @@ function fastify (options) {
697
707
}
698
708
699
709
function setNotFoundHandler ( opts , handler ) {
700
- throwIfAlreadyStarted ( 'Cannot call "setNotFoundHandler" when fastify instance is already started !' )
710
+ throwIfAlreadyStarted ( 'Cannot call "setNotFoundHandler"!' )
701
711
702
712
fourOhFour . setNotFoundHandler . call ( this , opts , handler , avvio , router . routeHandler )
703
713
return this
704
714
}
705
715
706
716
function setValidatorCompiler ( validatorCompiler ) {
707
- throwIfAlreadyStarted ( 'Cannot call "setValidatorCompiler" when fastify instance is already started !' )
717
+ throwIfAlreadyStarted ( 'Cannot call "setValidatorCompiler"!' )
708
718
this [ kSchemaController ] . setValidatorCompiler ( validatorCompiler )
709
719
return this
710
720
}
711
721
712
722
function setSchemaErrorFormatter ( errorFormatter ) {
713
- throwIfAlreadyStarted ( 'Cannot call "setSchemaErrorFormatter" when fastify instance is already started !' )
723
+ throwIfAlreadyStarted ( 'Cannot call "setSchemaErrorFormatter"!' )
714
724
validateSchemaErrorFormatter ( errorFormatter )
715
725
this [ kSchemaErrorFormatter ] = errorFormatter . bind ( this )
716
726
return this
717
727
}
718
728
719
729
function setSerializerCompiler ( serializerCompiler ) {
720
- throwIfAlreadyStarted ( 'Cannot call "setSerializerCompiler" when fastify instance is already started !' )
730
+ throwIfAlreadyStarted ( 'Cannot call "setSerializerCompiler"!' )
721
731
this [ kSchemaController ] . setSerializerCompiler ( serializerCompiler )
722
732
return this
723
733
}
724
734
725
735
function setSchemaController ( schemaControllerOpts ) {
726
- throwIfAlreadyStarted ( 'Cannot call "setSchemaController" when fastify instance is already started !' )
736
+ throwIfAlreadyStarted ( 'Cannot call "setSchemaController"!' )
727
737
const old = this [ kSchemaController ]
728
738
const schemaController = SchemaController . buildSchemaController ( old , Object . assign ( { } , old . opts , schemaControllerOpts ) )
729
739
this [ kSchemaController ] = schemaController
@@ -733,15 +743,15 @@ function fastify (options) {
733
743
}
734
744
735
745
function setReplySerializer ( replySerializer ) {
736
- throwIfAlreadyStarted ( 'Cannot call "setReplySerializer" when fastify instance is already started !' )
746
+ throwIfAlreadyStarted ( 'Cannot call "setReplySerializer"!' )
737
747
738
748
this [ kReplySerializerDefault ] = replySerializer
739
749
return this
740
750
}
741
751
742
752
// wrapper that we expose to the user for configure the custom error handler
743
753
function setErrorHandler ( func ) {
744
- throwIfAlreadyStarted ( 'Cannot call "setErrorHandler" when fastify instance is already started !' )
754
+ throwIfAlreadyStarted ( 'Cannot call "setErrorHandler"!' )
745
755
746
756
this [ kErrorHandler ] = buildErrorHandler ( this [ kErrorHandler ] , func . bind ( this ) )
747
757
return this
@@ -766,7 +776,8 @@ function fastify (options) {
766
776
if ( typeof url === 'string' ) {
767
777
req . url = url
768
778
} else {
769
- req . destroy ( new Error ( `Rewrite url for "${ req . url } " needs to be of type "string" but received "${ typeof url } "` ) )
779
+ const err = new FST_ERR_ROUTE_REWRITE_NOT_STR ( req . url , typeof url )
780
+ req . destroy ( err )
770
781
}
771
782
}
772
783
}
@@ -779,9 +790,9 @@ fastify.errorCodes = errorCodes
779
790
780
791
function validateSchemaErrorFormatter ( schemaErrorFormatter ) {
781
792
if ( typeof schemaErrorFormatter !== 'function' ) {
782
- throw new Error ( `schemaErrorFormatter option should be a function, instead got ${ typeof schemaErrorFormatter } ` )
793
+ throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN ( typeof schemaErrorFormatter )
783
794
} else if ( schemaErrorFormatter . constructor . name === 'AsyncFunction' ) {
784
- throw new Error ( 'schemaErrorFormatter option should not be an async function ')
795
+ throw new FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN ( 'AsyncFunction ')
785
796
}
786
797
}
787
798
0 commit comments