1+ import { ERRORS } from "./errors" ;
12import { FileMeta } from "../file/file" ;
23import { bytesToMB , isFloat } from "../utils/number" ;
34import { isPlainObject } from "../utils/validation" ;
@@ -119,9 +120,7 @@ export class MessageManager {
119120 constructor ( private generateID : ( ) => string ) { }
120121
121122 createSystemMessage ( params : CreateSystemMessageParams , userMsg ?: Message ) : Message {
122- if ( ! userMsg && ! params . chatID ) {
123- throw new Error ( "chatID is required when user message is not provided" ) ;
124- }
123+ if ( ! userMsg && ! params . chatID ) throw new Error ( ERRORS . SYSTEM_CHAT_ID_REQUIRED ) ;
125124
126125 return {
127126 id : this . generateID ( ) ,
@@ -143,18 +142,13 @@ export class MessageManager {
143142 } ;
144143
145144 if ( replyMsg ) {
146- msg = {
147- ...msg ,
148- replyTo : replyMsg . id ,
149- scene : replyMsg . scene ,
150- step : replyMsg . step ,
151- } ;
145+ msg = { ...msg , replyTo : replyMsg . id , scene : replyMsg . scene , step : replyMsg . step } ;
152146 }
153147
154148 if ( ! replyMsg ?. reply ) return msg ;
155149
156150 if ( replyMsg . reply . bodyType !== msg . body . type ) {
157- throw Error ( `Invalid body type. Expected ${ replyMsg . reply . bodyType } ` ) ;
151+ throw Error ( ERRORS . INVALID_BODY_TYPE_EXPECTED ( replyMsg . reply . bodyType ) ) ;
158152 }
159153
160154 if ( replyMsg . reply . bodyType === "text" ) {
@@ -163,13 +157,11 @@ export class MessageManager {
163157 msg . body . content = bodyContent ;
164158
165159 if ( limit ?. minLength != null && bodyContent . length < limit . minLength ) {
166- throw Error ( `The message body is too short. It must be at least ${ limit . minLength } characters long.` ) ;
160+ throw Error ( ERRORS . TEXT_TOO_SHORT ( limit . minLength ) ) ;
167161 }
168162
169163 if ( limit ?. maxLength != null && bodyContent . length > limit . maxLength ) {
170- throw Error (
171- `The message body is too long. It should be no more than ${ limit . maxLength } characters long.`
172- ) ;
164+ throw Error ( ERRORS . TEXT_TOO_LONG ( limit . maxLength ) ) ;
173165 }
174166 }
175167
@@ -179,15 +171,15 @@ export class MessageManager {
179171 msg . body . content = bodyContent ;
180172
181173 if ( limit ?. integerOnly && isFloat ( bodyContent ) ) {
182- throw Error ( `Invalid message body. It must be a valid integer.` ) ;
174+ throw Error ( ERRORS . NUMBER_NOT_INTEGER ) ;
183175 }
184176
185177 if ( limit ?. min != null && bodyContent < limit . min ) {
186- throw Error ( `The number is too small. The minimum allowed value is ${ limit . min } .` ) ;
178+ throw Error ( ERRORS . NUMBER_TOO_SMALL ( limit . min ) ) ;
187179 }
188180
189181 if ( limit ?. max != null && bodyContent > limit . max ) {
190- throw Error ( `The number is too big. The maximum allowed value is ${ limit . max } .` ) ;
182+ throw Error ( ERRORS . NUMBER_TOO_BIG ( limit . max ) ) ;
191183 }
192184 }
193185
@@ -197,11 +189,11 @@ export class MessageManager {
197189 msg . body . content = bodyContent ;
198190
199191 if ( limit ?. min != null && bodyContent . getTime ( ) < limit . min . getTime ( ) ) {
200- throw Error ( `The date is too old. The minimum allowed is ${ limit . min . toISOString ( ) } .` ) ;
192+ throw Error ( ERRORS . DATE_TOO_OLD ( limit . min ) ) ;
201193 }
202194
203195 if ( limit ?. max != null && bodyContent . getTime ( ) > limit . max . getTime ( ) ) {
204- throw Error ( `The date is too early. The maximum allowed is ${ limit . max . toISOString ( ) } .` ) ;
196+ throw Error ( ERRORS . DATE_TOO_EARLY ( limit . max ) ) ;
205197 }
206198 }
207199
@@ -210,7 +202,7 @@ export class MessageManager {
210202 const limit = replyMsg . reply . bodyLimits as OptionLimits ;
211203 const existingOption = limit ?. options ?. find ( ( opt ) => opt ?. value === bodyContent ) ;
212204 if ( ! existingOption ) {
213- throw Error ( "The provided option does not exist." ) ;
205+ throw Error ( ERRORS . OPTION_NOT_EXIST ) ;
214206 }
215207 }
216208
@@ -219,13 +211,13 @@ export class MessageManager {
219211 const limit = replyMsg . reply . bodyLimits as OptionsLimits ;
220212
221213 if ( limit ?. maxAmount != null && bodyContent . length > limit . maxAmount ) {
222- throw Error ( `Too many options. The maximum allowed is ${ limit . maxAmount } ` ) ;
214+ throw Error ( ERRORS . TOO_MANY_OPTIONS ( limit . maxAmount ) ) ;
223215 }
224216
225217 for ( const option of bodyContent ) {
226218 const existingOption = limit ?. options ?. find ( ( lopt ) => lopt ?. value === option ) ;
227219 if ( ! existingOption ) {
228- throw Error ( "The provided option does not exist." ) ;
220+ throw Error ( ERRORS . OPTION_NOT_EXIST ) ;
229221 }
230222 }
231223 }
@@ -240,7 +232,7 @@ export class MessageManager {
240232 const bodyContent = msg . body . content as FileMeta [ ] ;
241233 const limit = replyMsg . reply . bodyLimits as FilesLimits ;
242234 if ( limit ?. maxAmount != null && bodyContent . length > limit . maxAmount ) {
243- throw Error ( `Too many files. The maximum allowed is ${ limit . maxAmount } ` ) ;
235+ throw Error ( ERRORS . TOO_MANY_FILES ( limit . maxAmount ) ) ;
244236 }
245237
246238 for ( const file of bodyContent ) {
@@ -250,54 +242,49 @@ export class MessageManager {
250242
251243 return msg ;
252244 }
253-
254245 validateUserMessageParams ( input : any , senderID : string ) : CreateUserMessageParams {
255- if ( ! isPlainObject ( input ) ) throw Error ( "Input must be an object" ) ;
246+ if ( ! isPlainObject ( input ) ) throw Error ( ERRORS . INPUT_NOT_OBJECT ) ;
256247 const params = structuredClone ( input ) ;
257248
258- if ( ! ( "chatID" in input ) || typeof input . chatID !== "string" ) throw Error ( "'chatID' must be a string" ) ;
259- if ( ! ( "body" in input ) || ! isPlainObject ( input . body ) ) throw Error ( "'body' must be an object" ) ;
260- if ( ! ( "type" in input . body ) || typeof input . body . type !== "string" ) throw Error ( "'body.type' must be a string" ) ;
261- if ( ! allowedUserMessageBodyTypes . includes ( input . body . type ) ) throw Error ( "Not allowed 'body.type'" ) ;
262- if ( ! ( "content" in input . body ) ) throw Error ( "'body.content' must be provided" ) ;
263- if ( "replyTo" in input && typeof input . replyTo !== "string" ) {
264- throw Error ( "'replyTo' must be a string if provided" ) ;
265- }
266-
267- const invalidBodyContentMsg = "Invalid body.content for the provided body.type" ;
249+ if ( ! ( "chatID" in input ) || typeof input . chatID !== "string" ) throw Error ( ERRORS . CHAT_ID_STRING ) ;
250+ if ( ! ( "body" in input ) || ! isPlainObject ( input . body ) ) throw Error ( ERRORS . BODY_NOT_OBJECT ) ;
251+ if ( ! ( "type" in input . body ) || typeof input . body . type !== "string" ) throw Error ( ERRORS . BODY_TYPE_STRING ) ;
252+ if ( ! ( "content" in input . body ) ) throw Error ( ERRORS . BODY_CONTENT_REQUIRED ) ;
253+ if ( "replyTo" in input && typeof input . replyTo !== "string" ) throw Error ( ERRORS . REPLY_TO_STRING ) ;
254+ if ( ! allowedUserMessageBodyTypes . includes ( input . body . type ) ) throw Error ( ERRORS . BODY_TYPE_NOT_ALLOWED ) ;
268255
269256 if ( input . body . type === "text" ) {
270- if ( typeof input . body . content !== "string" ) throw Error ( invalidBodyContentMsg ) ;
257+ if ( typeof input . body . content !== "string" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
271258 }
272259 if ( input . body . type === "number" ) {
273- if ( typeof input . body . content !== "number" ) throw Error ( invalidBodyContentMsg ) ;
260+ if ( typeof input . body . content !== "number" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
274261 }
275262 if ( input . body . type === "boolean" ) {
276- if ( typeof input . body . content !== "boolean" ) throw Error ( invalidBodyContentMsg ) ;
263+ if ( typeof input . body . content !== "boolean" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
277264 }
278265 if ( input . body . type === "datetime" ) {
279- if ( typeof input . body . content !== "string" ) throw Error ( invalidBodyContentMsg ) ;
280- if ( isNaN ( new Date ( input . body . content ) ?. getTime ( ) ) ) throw Error ( invalidBodyContentMsg ) ;
266+ if ( typeof input . body . content !== "string" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
267+ if ( isNaN ( new Date ( input . body . content ) ?. getTime ( ) ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
281268 params . body . content = new Date ( input . body . content ) ;
282269 }
283270 if ( input . body . type === "option" ) {
284- if ( typeof input . body . content !== "string" ) throw Error ( invalidBodyContentMsg ) ;
271+ if ( typeof input . body . content !== "string" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
285272 }
286273 if ( input . body . type === "options" ) {
287- if ( ! Array . isArray ( input . body . content ) ) throw Error ( invalidBodyContentMsg ) ;
274+ if ( ! Array . isArray ( input . body . content ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
288275 params . body . content = input . body . content . map ( String ) ;
289276 }
290277 if ( input . body . type === "file" ) {
291- if ( ! isPlainObject ( input . body . content ) ) throw Error ( invalidBodyContentMsg ) ;
292- if ( ! ( "id" in input . body . content ) ) throw Error ( invalidBodyContentMsg ) ;
293- if ( typeof input . body . content . id !== "string" ) throw Error ( invalidBodyContentMsg ) ;
278+ if ( ! isPlainObject ( input . body . content ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
279+ if ( ! ( "id" in input . body . content ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
280+ if ( typeof input . body . content . id !== "string" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
294281 }
295282 if ( input . body . type === "files" ) {
296- if ( ! Array . isArray ( input . body . content ) ) throw Error ( invalidBodyContentMsg ) ;
283+ if ( ! Array . isArray ( input . body . content ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
297284 for ( const item of input . body . content ) {
298- if ( ! isPlainObject ( item ) ) throw Error ( invalidBodyContentMsg ) ;
299- if ( ! ( "id" in item ) ) throw Error ( invalidBodyContentMsg ) ;
300- if ( typeof item . id !== "string" ) throw Error ( invalidBodyContentMsg ) ;
285+ if ( ! isPlainObject ( item ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
286+ if ( ! ( "id" in item ) ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
287+ if ( typeof item . id !== "string" ) throw Error ( ERRORS . INVALID_BODY_CONTENT ) ;
301288 }
302289 }
303290
@@ -312,11 +299,11 @@ export class MessageManager {
312299 private checkFileDataLimit ( limit : FileLimits , filemeta : FileMeta ) {
313300 if ( limit ?. mimeTypes && limit . mimeTypes . length ) {
314301 const isValidMime = limit . mimeTypes . includes ( filemeta . mimeType ) ;
315- if ( ! isValidMime ) throw Error ( "MIME type not allowed." ) ;
302+ if ( ! isValidMime ) throw Error ( ERRORS . MIME_NOT_ALLOWED ) ;
316303 }
317304
318305 if ( limit ?. maxSize != null && filemeta . size > limit . maxSize ) {
319- throw Error ( `Too large file. The maximum allowed size is ${ bytesToMB ( limit . maxSize ) } MB.` ) ;
306+ throw Error ( ERRORS . FILE_TOO_LARGE ( bytesToMB ( limit . maxSize ) ) ) ;
320307 }
321308 }
322309}
0 commit comments