Skip to content

Commit 97f7e97

Browse files
authored
Merge branch 'main' into v2/use-plugin-for-examples
2 parents cc1783b + 12b2ee2 commit 97f7e97

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

.github/workflows/pull_request.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,31 @@ jobs:
2626
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2727
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2828

29-
examples:
29+
construct-examples-matrix:
30+
name: Construct Examples matrix
31+
runs-on: ubuntu-latest
32+
outputs:
33+
examples-matrix: '${{ steps.generate-matrix.outputs.examples-matrix }}'
34+
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
37+
with:
38+
persist-credentials: false
39+
- id: generate-matrix
40+
run: echo "examples-matrix=$(curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/generate_matrix.sh | bash)" >> "$GITHUB_OUTPUT"
41+
env:
42+
MATRIX_LINUX_5_9_ENABLED: false
43+
MATRIX_LINUX_5_10_ENABLED: false
44+
MATRIX_LINUX_COMMAND: "./dev/build-examples.sh"
45+
MATRIX_LINUX_SETUP_COMMAND: "apt update && apt install -y protobuf-compiler && ./dev/build-examples.sh"
46+
47+
examples-matrix:
3048
name: Examples
31-
uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main
49+
needs: construct-examples-matrix
50+
uses: apple/swift-nio/.github/workflows/swift_test_matrix.yml@main
3251
with:
3352
name: "Examples"
34-
matrix_linux_5_9_enabled: false
35-
matrix_linux_5_10_enabled: false
36-
matrix_linux_command: "apt update && apt install -y protobuf-compiler && ./dev/build-examples.sh"
53+
matrix_string: '${{ needs.construct-examples-matrix.outputs.examples-matrix }}'
3754

3855
benchmarks:
3956
name: Benchmarks

Sources/GRPCCore/Status.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,32 @@ extension Status.Code {
296296
/// operation.
297297
public static let unauthenticated = Self(code: .unauthenticated)
298298
}
299+
300+
extension Status {
301+
/// Create a status from an HTTP status code for a response which didn't include a gRPC status.
302+
///
303+
/// - Parameter httpStatusCode: The HTTP status code to map to a status.
304+
public init(httpStatusCode: Int) {
305+
// See the "http-grpc-status-mapping.md" doc in grpc/grpc GitHub repo.
306+
switch httpStatusCode {
307+
case 400:
308+
self = Status(code: .internalError, message: "HTTP 400: Bad Request")
309+
case 401:
310+
self = Status(code: .unauthenticated, message: "HTTP 401: Unauthorized")
311+
case 403:
312+
self = Status(code: .permissionDenied, message: "HTTP 403: Forbidden")
313+
case 404:
314+
self = Status(code: .unimplemented, message: "HTTP 404: Not Found")
315+
case 429:
316+
self = Status(code: .unavailable, message: "HTTP 429: Too Many Requests")
317+
case 502:
318+
self = Status(code: .unavailable, message: "HTTP 502: Bad Gateway")
319+
case 503:
320+
self = Status(code: .unavailable, message: "HTTP 503: Service Unavailable")
321+
case 504:
322+
self = Status(code: .unavailable, message: "HTTP 504: Gateway Timeout")
323+
default:
324+
self = Status(code: .unknown, message: "HTTP \(httpStatusCode)")
325+
}
326+
}
327+
}

Tests/GRPCCoreTests/StatusTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,23 @@ struct StatusTests {
6969
func fitsInExistentialContainer() {
7070
#expect(MemoryLayout<Status>.size <= 24)
7171
}
72+
73+
@Test(
74+
"From HTTP status code",
75+
arguments: [
76+
(400, Status(code: .internalError, message: "HTTP 400: Bad Request")),
77+
(401, Status(code: .unauthenticated, message: "HTTP 401: Unauthorized")),
78+
(403, Status(code: .permissionDenied, message: "HTTP 403: Forbidden")),
79+
(404, Status(code: .unimplemented, message: "HTTP 404: Not Found")),
80+
(429, Status(code: .unavailable, message: "HTTP 429: Too Many Requests")),
81+
(502, Status(code: .unavailable, message: "HTTP 502: Bad Gateway")),
82+
(503, Status(code: .unavailable, message: "HTTP 503: Service Unavailable")),
83+
(504, Status(code: .unavailable, message: "HTTP 504: Gateway Timeout")),
84+
(418, Status(code: .unknown, message: "HTTP 418")),
85+
]
86+
)
87+
func convertFromHTTPStatusCode(code: Int, expected: Status) {
88+
let status = Status(httpStatusCode: code)
89+
#expect(status == expected)
90+
}
7291
}

0 commit comments

Comments
 (0)