Skip to content

Commit c8f5a3a

Browse files
author
Chase Coalwell
authored
feat: deserialize with error header (#385)
1 parent 2b77246 commit c8f5a3a

File tree

1 file changed

+93
-55
lines changed

1 file changed

+93
-55
lines changed

clients/node/client-rds-data-node/protocol/AwsRestJson1_1.ts

Lines changed: 93 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {
33
ExecuteStatementRequest,
44
ExecuteStatementResponse,
55
Field,
6-
SqlParameter
6+
SqlParameter,
7+
BadRequestException,
8+
StatementTimeoutException,
9+
ForbiddenException,
10+
InternalServerErrorException,
11+
ServiceUnavailableError
712
} from "../models/rdsdataservice";
813
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
914
import * as __aws_sdk_stream_collector_node from "@aws-sdk/stream-collector-node";
@@ -60,13 +65,13 @@ export function executeStatementAwsRestJson1_1Serialize(
6065
});
6166
}
6267

63-
export function executeStatementAwsRestJson1_1Deserialize(
68+
export async function executeStatementAwsRestJson1_1Deserialize(
6469
output: HttpResponse
6570
): Promise<ExecuteStatementResponse> {
6671
if (output.statusCode !== 200) {
6772
return executeStatementAwsRestJson1_1DeserializeError(output);
6873
}
69-
let data: any = parseBody(output.body);
74+
let data: any = await parseBody(output.body);
7075
return Promise.resolve({
7176
__type: "com.amazon.rdsdataservice#ExecuteStatementResponse",
7277
records: recordsAwsRestJson1_1Deserialize(data.records),
@@ -80,59 +85,42 @@ export function executeStatementAwsRestJson1_1Deserialize(
8085
});
8186
}
8287

83-
function executeStatementAwsRestJson1_1DeserializeError(
88+
async function executeStatementAwsRestJson1_1DeserializeError(
8489
output: HttpResponse
8590
): Promise<ExecuteStatementResponse> {
86-
let data = parseBody(output);
87-
if (output.statusCode === 400 && data.dbConnectionId !== undefined) {
88-
return Promise.reject({
89-
__type: "com.amazon.rdsdataservice#StatementTimeoutException",
90-
$name: "StatementTimeoutException",
91-
$fault: "client",
92-
message: data.message,
93-
dbConnectionId: data.dbConnectionId
94-
});
95-
}
96-
97-
if (output.statusCode === 400) {
98-
return Promise.reject({
99-
__type: "com.amazon.rdsdataservice#BadRequestException",
100-
$name: "BadRequestException",
101-
$fault: "client",
102-
message: data.message
103-
});
104-
}
105-
106-
if (output.statusCode === 403) {
107-
return Promise.reject({
108-
__type: "com.amazon.rdsdataservice#ForbiddenException",
109-
$name: "ForbiddenException",
110-
$fault: "client",
111-
message: data.message
112-
});
113-
}
114-
115-
if (output.statusCode === 500) {
116-
return Promise.reject({
117-
__type: "com.amazon.rdsdataservice#InternalServerErrorException",
118-
$name: "InternalServerErrorException",
119-
$fault: "server"
120-
});
121-
}
122-
123-
if (output.statusCode === 503) {
124-
return Promise.reject({
125-
__type: "com.amazon.rdsdataservice#ServiceUnavailableError",
126-
$name: "ServiceUnavailableError",
127-
$fault: "server"
128-
});
129-
}
130-
131-
return Promise.reject({
132-
__type: "com.amazon.rdsdataservice#UnknownException",
133-
$name: "UnknownException",
134-
$fault: "server"
135-
});
91+
const data: any = await parseBody(output);
92+
93+
let response: any;
94+
switch (output.headers["x-amzn-ErrorType"]) {
95+
case "BadRequestException":
96+
case "com.amazon.rdsdataservice#BadRequestException":
97+
response = badRequestExceptionDeserialize(data);
98+
break;
99+
case "StatementTimeoutException":
100+
case "com.amazon.rdsdataservice#StatementTimeoutException":
101+
response = statementTimeoutExceptionDeserialize(data);
102+
break;
103+
case "ForbiddenException":
104+
case "com.amazon.rdsdataservice#ForbiddenException":
105+
response = forbiddenExceptionDeserialize(data);
106+
break;
107+
case "InternalServerErrorException":
108+
case "com.amazon.rdsdataservice#InternalServerErrorException":
109+
response = internalServerErrorExceptionDeserialize(data);
110+
break;
111+
case "ServiceUnavailableError":
112+
case "com.amazon.rdsdataservice#ServiceUnavailableError":
113+
response = serviceUnavailableErrorDeserialize(data);
114+
break;
115+
default:
116+
response = {
117+
__type: "com.amazon.rdsdataservice#UnknownException",
118+
$name: "UnknownException",
119+
$fault: "server"
120+
};
121+
}
122+
123+
return Promise.reject(response);
136124
}
137125

138126
function sqlParameterListAwsRestJson1_1Serialize(
@@ -290,8 +278,58 @@ function recordsListAwsRestJson1_1Deserialize(input: any): Array<Field> {
290278
return list;
291279
}
292280

281+
function badRequestExceptionDeserialize(input: any): BadRequestException {
282+
return {
283+
__type: "com.amazon.rdsdataservice#BadRequestException",
284+
$name: "BadRequestException",
285+
$fault: "client",
286+
message: input.message
287+
};
288+
}
289+
290+
function statementTimeoutExceptionDeserialize(
291+
input: any
292+
): StatementTimeoutException {
293+
return {
294+
__type: "com.amazon.rdsdataservice#StatementTimeoutException",
295+
$name: "StatementTimeoutException",
296+
$fault: "client",
297+
message: input.message,
298+
dbConnectionId: input.dbConnectionId
299+
};
300+
}
301+
302+
function forbiddenExceptionDeserialize(input: any): ForbiddenException {
303+
return {
304+
__type: "com.amazon.rdsdataservice#ForbiddenException",
305+
$name: "ForbiddenException",
306+
$fault: "client",
307+
message: input.message
308+
};
309+
}
310+
311+
function internalServerErrorExceptionDeserialize(
312+
input: any
313+
): InternalServerErrorException {
314+
return {
315+
__type: "com.amazon.rdsdataservice#InternalServerErrorException",
316+
$name: "InternalServerErrorException",
317+
$fault: "server"
318+
};
319+
}
320+
321+
function serviceUnavailableErrorDeserialize(
322+
input: any
323+
): ServiceUnavailableError {
324+
return {
325+
__type: "com.amazon.rdsdataservice#ServiceUnavailableError",
326+
$name: "ServiceUnavailableError",
327+
$fault: "server"
328+
};
329+
}
330+
293331
function parseBody(streamBody: any): any {
294332
__aws_sdk_stream_collector_node.streamCollector(streamBody).then(body => {
295-
return __aws_sdk_util_utf8_node.toUtf8(body);
333+
return JSON.parse(__aws_sdk_util_utf8_node.toUtf8(body));
296334
});
297335
}

0 commit comments

Comments
 (0)