MCPcopy Create free account
hub / github.com/middleapi/orpc / #errorResponse

Method #errorResponse

packages/openapi/src/openapi-generator.ts:547–607  ·  view source on GitHub ↗
(
    ref: OpenAPI.OperationObject,
    def: AnyContractProcedure['~orpc'],
    baseSchemaConvertOptions: Pick<SchemaConvertOptions, 'components'>,
    undefinedErrorSchema: JSONSchema,
    customErrorResponseBodySchema: OpenAPIGeneratorGenerateOptions['customErrorResponseBodySchema'],
  )

Source from the content-addressed store, hash-verified

545 }
546
547 async #errorResponse(
548 ref: OpenAPI.OperationObject,
549 def: AnyContractProcedure['~orpc'],
550 baseSchemaConvertOptions: Pick<SchemaConvertOptions, 'components'>,
551 undefinedErrorSchema: JSONSchema,
552 customErrorResponseBodySchema: OpenAPIGeneratorGenerateOptions['customErrorResponseBodySchema'],
553 ): Promise<void> {
554 const errorMap = def.errorMap as ErrorMap
555
556 const errorResponsesByStatus: Record<string, {
557 status: number
558 errorSchemaVariants: JSONSchema[]
559 definedErrorDefinitions: [code: string, defaultMessage: string, required: boolean, schema: JSONSchema][]
560 }> = {}
561
562 for (const code in errorMap) {
563 const config = errorMap[code]
564
565 if (!config) {
566 continue
567 }
568
569 const status = fallbackORPCErrorStatus(code, config.status)
570 const defaultMessage = fallbackORPCErrorMessage(code, config.message)
571
572 errorResponsesByStatus[status] ??= { status, definedErrorDefinitions: [], errorSchemaVariants: [] }
573
574 const [dataRequired, dataSchema] = await this.converter.convert(config.data, { ...baseSchemaConvertOptions, strategy: 'output' })
575
576 errorResponsesByStatus[status].definedErrorDefinitions.push([code, defaultMessage, dataRequired, dataSchema])
577 errorResponsesByStatus[status].errorSchemaVariants.push({
578 type: 'object',
579 properties: {
580 defined: { const: true },
581 code: { const: code },
582 status: { const: status },
583 message: { type: 'string', default: defaultMessage },
584 data: dataSchema,
585 },
586 required: dataRequired ? ['defined', 'code', 'status', 'message', 'data'] : ['defined', 'code', 'status', 'message'],
587 })
588 }
589
590 ref.responses ??= {}
591
592 for (const statusString in errorResponsesByStatus) {
593 const errorResponse = errorResponsesByStatus[statusString]!
594
595 const customBodySchema = value(customErrorResponseBodySchema, errorResponse.definedErrorDefinitions, errorResponse.status)
596
597 ref.responses[statusString] = {
598 description: statusString,
599 content: toOpenAPIContent(customBodySchema ?? {
600 oneOf: [
601 ...errorResponse.errorSchemaVariants,
602 undefinedErrorSchema,
603 ],
604 }),

Callers 1

generateMethod · 0.95

Calls 6

fallbackORPCErrorStatusFunction · 0.90
fallbackORPCErrorMessageFunction · 0.90
valueFunction · 0.90
toOpenAPIContentFunction · 0.90
pushMethod · 0.80
convertMethod · 0.65

Tested by

no test coverage detected