|
| 1 | +import { TypeRegistry } from "@smithy/core/schema"; |
1 | 2 | import { HttpResponse } from "@smithy/protocol-http"; |
| 3 | +import { ServiceException, ServiceExceptionOptions } from "@smithy/smithy-client"; |
| 4 | +import { StaticErrorSchema } from "@smithy/types"; |
2 | 5 | import { describe, expect, test as it } from "vitest"; |
3 | 6 |
|
4 | 7 | import { context } from "../test-schema.spec"; |
5 | 8 | import { AwsQueryProtocol } from "./AwsQueryProtocol"; |
6 | 9 |
|
7 | 10 | describe(AwsQueryProtocol.name, () => { |
| 11 | + class GiraffeServiceException extends ServiceException { |
| 12 | + constructor(options: ServiceExceptionOptions) { |
| 13 | + super(options); |
| 14 | + Object.setPrototypeOf(this, GiraffeServiceException.prototype); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + const syntheticNamespace = "smithy.ts.sdk.synthetic.com.amazonaws.giraffes"; |
| 19 | + TypeRegistry.for(syntheticNamespace).registerError( |
| 20 | + [-3, syntheticNamespace, "GiraffeServiceException", 0, [], []] satisfies StaticErrorSchema, |
| 21 | + GiraffeServiceException |
| 22 | + ); |
| 23 | + |
8 | 24 | it("decorates service exceptions with unmodeled fields", async () => { |
9 | 25 | const httpResponse = new HttpResponse({ |
10 | 26 | statusCode: 400, |
@@ -41,4 +57,72 @@ describe(AwsQueryProtocol.name, () => { |
41 | 57 | }, |
42 | 58 | }); |
43 | 59 | }); |
| 60 | + |
| 61 | + it("should copy Error.Code to error.name and Error.Message to error.message", async () => { |
| 62 | + const httpResponse = new HttpResponse({ |
| 63 | + statusCode: 400, |
| 64 | + headers: {}, |
| 65 | + body: Buffer.from( |
| 66 | + "<Exception>" + |
| 67 | + "<Error>" + |
| 68 | + "<Type>Sender</Type>" + |
| 69 | + "<Code>GiraffeServiceException</Code>" + |
| 70 | + "<Message>A giraffe has eaten your umbrella</Message>" + |
| 71 | + "</Error>" + |
| 72 | + "</Exception>" |
| 73 | + ), |
| 74 | + }); |
| 75 | + |
| 76 | + const protocol = new AwsQueryProtocol({ |
| 77 | + version: "1999-12-31", |
| 78 | + defaultNamespace: "com.amazonaws.giraffes", |
| 79 | + xmlNamespace: "ns", |
| 80 | + }); |
| 81 | + |
| 82 | + const actual = await protocol |
| 83 | + .deserializeResponse( |
| 84 | + { |
| 85 | + namespace: "ns", |
| 86 | + name: "Empty", |
| 87 | + traits: 0, |
| 88 | + input: "unit" as const, |
| 89 | + output: [3, "ns", "EmptyOutput", 0, [], []], |
| 90 | + }, |
| 91 | + context, |
| 92 | + httpResponse |
| 93 | + ) |
| 94 | + .catch((e) => { |
| 95 | + return e; |
| 96 | + }); |
| 97 | + |
| 98 | + const expected = Object.assign( |
| 99 | + new GiraffeServiceException({ |
| 100 | + name: "GiraffeServiceException", |
| 101 | + $fault: "client", |
| 102 | + message: "A giraffe has eaten your umbrella", |
| 103 | + $metadata: { |
| 104 | + cfId: undefined, |
| 105 | + extendedRequestId: undefined, |
| 106 | + httpStatusCode: 400, |
| 107 | + requestId: undefined, |
| 108 | + }, |
| 109 | + }), |
| 110 | + { |
| 111 | + Type: "Sender", |
| 112 | + Code: "GiraffeServiceException", |
| 113 | + Error: { |
| 114 | + Code: "GiraffeServiceException", |
| 115 | + Message: "A giraffe has eaten your umbrella", |
| 116 | + Type: "Sender", |
| 117 | + }, |
| 118 | + } |
| 119 | + ); |
| 120 | + |
| 121 | + expect(actual.constructor).toBe(expected.constructor); |
| 122 | + expect(actual.name).toEqual(expected.name); |
| 123 | + expect(actual.message).toEqual(expected.message); |
| 124 | + expect(actual.Type).toEqual(expected.Type); |
| 125 | + expect(actual.Code).toEqual(expected.Code); |
| 126 | + expect(actual.Error).toEqual(expected.Error); |
| 127 | + }); |
44 | 128 | }); |
0 commit comments