Skip to content

Commit b5414e3

Browse files
committed
Fix APIGateway.Response with EncodingError
1 parent 1a3eaf6 commit b5414e3

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

Sources/TencentSCFEvents/APIGateway/Response+Init.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ extension APIGateway.Response {
3131
encoding: .utf8
3232
) ?? ""
3333
self.statusCode = statusCode
34+
} catch let err as EncodingError {
35+
self.body = #"{"error":"EncodingError","message":"\#("\(err)".jsonEncoded())"}"#
36+
self.statusCode = .internalServerError
3437
} catch let err {
35-
self.body = #"{"errorType":"FunctionError","errorMsg":"\#(err.localizedDescription)"}"#
38+
self.body = #"{"error":"UnexpectedError","message":"\#("\(err)".jsonEncoded())"}"#
3639
self.statusCode = .internalServerError
3740
}
3841
self.isBase64Encoded = false
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===------------------------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftTencentSCFRuntime open source project
4+
//
5+
// Copyright (c) 2020 stevapple and the SwiftTencentSCFRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftTencentSCFRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===------------------------------------------------------------------------------------===//
14+
//
15+
// This source file was part of the SwiftAWSLambdaRuntime open source project
16+
//
17+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
18+
// Licensed under Apache License v2.0
19+
//
20+
// See LICENSE.txt for license information
21+
// See http://github.com/swift-server/swift-aws-lambda-runtime/blob/master/CONTRIBUTORS.txt
22+
// for the list of SwiftAWSLambdaRuntime project authors
23+
//
24+
// SPDX-License-Identifier: Apache-2.0
25+
//
26+
//===------------------------------------------------------------------------------------===//
27+
28+
internal extension String {
29+
func jsonEncoded() -> String {
30+
var bytes = [UInt8]()
31+
let stringBytes = self.utf8
32+
var startCopyIndex = stringBytes.startIndex
33+
var nextIndex = startCopyIndex
34+
35+
while nextIndex != stringBytes.endIndex {
36+
switch stringBytes[nextIndex] {
37+
case 0 ..< 32, UInt8(ascii: "\""), UInt8(ascii: "\\"):
38+
// All Unicode characters may be placed within the
39+
// quotation marks, except for the characters that MUST be escaped:
40+
// quotation mark, reverse solidus, and the control characters (U+0000
41+
// through U+001F).
42+
// https://tools.ietf.org/html/rfc7159#section-7
43+
// copy the current range over
44+
bytes.append(contentsOf: stringBytes[startCopyIndex ..< nextIndex])
45+
bytes.append(UInt8(ascii: "\\"))
46+
bytes.append(stringBytes[nextIndex])
47+
48+
nextIndex = stringBytes.index(after: nextIndex)
49+
startCopyIndex = nextIndex
50+
default:
51+
nextIndex = stringBytes.index(after: nextIndex)
52+
}
53+
}
54+
55+
return String(bytes: bytes, encoding: .utf8)!
56+
}
57+
}

Tests/TencentSCFEventsTests/APIGatewayTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,33 @@ class APIGatewayTests: XCTestCase {
275275
XCTFail("Expect output JSON")
276276
}
277277
}
278+
279+
func testResponseEncodingWithEncodingError() {
280+
struct NotReallyEncodable: Encodable {
281+
let value: String = "NotReallyEncodable"
282+
283+
func encode(to encoder: Encoder) throws {
284+
throw EncodingError.invalidValue(self.value, .init(codingPath: encoder.codingPath, debugDescription: "You're testing something not really Encodable! Good luck."))
285+
}
286+
}
287+
288+
let encoder = JSONEncoder()
289+
encoder.outputFormatting = .sortedKeys
290+
291+
let resp = APIGateway.Response(
292+
statusCode: .ok,
293+
codableBody: NotReallyEncodable()
294+
)
295+
let expectedJson = #"{"body":"{\"error\":\"EncodingError\",\"message\":\"invalidValue(\\\"NotReallyEncodable\\\", Swift.EncodingError.Context(codingPath: [], debugDescription: \\\"You\\\\'re testing something not really Encodable! Good luck.\\\"\"}","headers":{"Content-Type":"application\/json"},"isBase64Encoded":false,"statusCode":500}"#
296+
297+
var data: Data?
298+
XCTAssertNoThrow(data = try encoder.encode(resp))
299+
if let data = data,
300+
let json = String(data: data, encoding: .utf8)
301+
{
302+
XCTAssertEqual(json, expectedJson)
303+
} else {
304+
XCTFail("Expect output JSON")
305+
}
306+
}
278307
}

0 commit comments

Comments
 (0)