|
| 1 | +import { ErrorSchema, NormalizedSchema, TypeRegistry } from "@smithy/core/schema"; |
| 2 | +import type { |
| 3 | + BodyLengthCalculator, |
| 4 | + HttpResponse as IHttpResponse, |
| 5 | + MetadataBearer, |
| 6 | + ResponseMetadata, |
| 7 | + SerdeFunctions, |
| 8 | +} from "@smithy/types"; |
| 9 | +import { calculateBodyLength } from "@smithy/util-body-length-browser"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +type ErrorMetadataBearer = MetadataBearer & { |
| 15 | + $response: IHttpResponse; |
| 16 | + $fault: "client" | "server"; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Shared code for Protocols. |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +export class ProtocolLib { |
| 25 | + /** |
| 26 | + * @param body - to be inspected. |
| 27 | + * @param serdeContext - this is a subset type but in practice is the client.config having a property called bodyLengthChecker. |
| 28 | + * |
| 29 | + * @returns content-length value for the body if possible. |
| 30 | + * @throws Error and should be caught and handled if not possible to determine length. |
| 31 | + */ |
| 32 | + public calculateContentLength(body: any, serdeContext?: SerdeFunctions) { |
| 33 | + const bodyLengthCalculator: BodyLengthCalculator = |
| 34 | + ( |
| 35 | + serdeContext as SerdeFunctions & { |
| 36 | + bodyLengthChecker?: BodyLengthCalculator; |
| 37 | + } |
| 38 | + )?.bodyLengthChecker ?? calculateBodyLength; |
| 39 | + return String(bodyLengthCalculator(body)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * This is only for REST protocols. |
| 44 | + * |
| 45 | + * @param defaultContentType - of the protocol. |
| 46 | + * @param inputSchema - schema for which to determine content type. |
| 47 | + * |
| 48 | + * @returns content-type header value or undefined when not applicable. |
| 49 | + */ |
| 50 | + public resolveRestContentType(defaultContentType: string, inputSchema: NormalizedSchema): string | undefined { |
| 51 | + const members = inputSchema.getMemberSchemas(); |
| 52 | + const httpPayloadMember = Object.values(members).find((m) => { |
| 53 | + return !!m.getMergedTraits().httpPayload; |
| 54 | + }); |
| 55 | + |
| 56 | + if (httpPayloadMember) { |
| 57 | + const mediaType = httpPayloadMember.getMergedTraits().mediaType as string; |
| 58 | + if (mediaType) { |
| 59 | + return mediaType; |
| 60 | + } else if (httpPayloadMember.isStringSchema()) { |
| 61 | + return "text/plain"; |
| 62 | + } else if (httpPayloadMember.isBlobSchema()) { |
| 63 | + return "application/octet-stream"; |
| 64 | + } else { |
| 65 | + return defaultContentType; |
| 66 | + } |
| 67 | + } else if (!inputSchema.isUnitSchema()) { |
| 68 | + const hasBody = Object.values(members).find((m) => { |
| 69 | + const { httpQuery, httpQueryParams, httpHeader, httpLabel, httpPrefixHeaders } = m.getMergedTraits(); |
| 70 | + return !httpQuery && !httpQueryParams && !httpHeader && !httpLabel && httpPrefixHeaders === void 0; |
| 71 | + }); |
| 72 | + if (hasBody) { |
| 73 | + return defaultContentType; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Shared code for finding error schema or throwing an unmodeled base error. |
| 80 | + * @returns error schema and error metadata. |
| 81 | + * |
| 82 | + * @throws ServiceBaseException or generic Error if no error schema could be found. |
| 83 | + */ |
| 84 | + public async getErrorSchemaOrThrowBaseException( |
| 85 | + errorIdentifier: string, |
| 86 | + defaultNamespace: string, |
| 87 | + response: IHttpResponse, |
| 88 | + dataObject: any, |
| 89 | + metadata: ResponseMetadata, |
| 90 | + getErrorSchema?: (registry: TypeRegistry, errorName: string) => ErrorSchema |
| 91 | + ): Promise<{ errorSchema: ErrorSchema; errorMetadata: ErrorMetadataBearer }> { |
| 92 | + let namespace = defaultNamespace; |
| 93 | + let errorName = errorIdentifier; |
| 94 | + if (errorIdentifier.includes("#")) { |
| 95 | + [namespace, errorName] = errorIdentifier.split("#"); |
| 96 | + } |
| 97 | + |
| 98 | + const errorMetadata: ErrorMetadataBearer = { |
| 99 | + $metadata: metadata, |
| 100 | + $response: response, |
| 101 | + $fault: response.statusCode < 500 ? ("client" as const) : ("server" as const), |
| 102 | + }; |
| 103 | + |
| 104 | + const registry = TypeRegistry.for(namespace); |
| 105 | + |
| 106 | + try { |
| 107 | + const errorSchema = getErrorSchema?.(registry, errorName) ?? (registry.getSchema(errorIdentifier) as ErrorSchema); |
| 108 | + return { errorSchema, errorMetadata }; |
| 109 | + } catch (e) { |
| 110 | + if (dataObject.Message) { |
| 111 | + dataObject.message = dataObject.Message; |
| 112 | + } |
| 113 | + const baseExceptionSchema = TypeRegistry.for("smithy.ts.sdk.synthetic." + namespace).getBaseException(); |
| 114 | + if (baseExceptionSchema) { |
| 115 | + const ErrorCtor = baseExceptionSchema.ctor; |
| 116 | + throw Object.assign(new ErrorCtor({ name: errorName }), errorMetadata, dataObject); |
| 117 | + } |
| 118 | + throw Object.assign(new Error(errorName), errorMetadata, dataObject); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Reads the x-amzn-query-error header for awsQuery compatibility. |
| 124 | + * |
| 125 | + * @param output - values that will be assigned to an error object. |
| 126 | + * @param response - from which to read awsQueryError headers. |
| 127 | + */ |
| 128 | + public setQueryCompatError(output: Record<string, any>, response: IHttpResponse) { |
| 129 | + const queryErrorHeader = response.headers?.["x-amzn-query-error"]; |
| 130 | + |
| 131 | + if (output !== undefined && queryErrorHeader != null) { |
| 132 | + const [Code, Type] = queryErrorHeader.split(";"); |
| 133 | + const entries = Object.entries(output); |
| 134 | + const Error = { |
| 135 | + Code, |
| 136 | + Type, |
| 137 | + } as any; |
| 138 | + Object.assign(output, Error); |
| 139 | + for (const [k, v] of entries) { |
| 140 | + Error[k] = v; |
| 141 | + } |
| 142 | + delete Error.__type; |
| 143 | + output.Error = Error; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Assigns Error, Type, Code from the awsQuery error object to the output error object. |
| 149 | + * @param queryCompatErrorData - query compat error object. |
| 150 | + * @param errorData - canonical error object returned to the caller. |
| 151 | + */ |
| 152 | + public queryCompatOutput(queryCompatErrorData: any, errorData: any) { |
| 153 | + if (queryCompatErrorData.Error) { |
| 154 | + errorData.Error = queryCompatErrorData.Error; |
| 155 | + } |
| 156 | + if (queryCompatErrorData.Type) { |
| 157 | + errorData.Type = queryCompatErrorData.Type; |
| 158 | + } |
| 159 | + if (queryCompatErrorData.Code) { |
| 160 | + errorData.Code = queryCompatErrorData.Code; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments