Skip to content

Commit

Permalink
Merge branch 'main' into v2/use-plugin-for-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
glbrntt authored Jan 22, 2025
2 parents cc1783b + 12b2ee2 commit 97f7e97
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
27 changes: 22 additions & 5 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,31 @@ jobs:
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"

examples:
construct-examples-matrix:
name: Construct Examples matrix
runs-on: ubuntu-latest
outputs:
examples-matrix: '${{ steps.generate-matrix.outputs.examples-matrix }}'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false
- id: generate-matrix
run: echo "examples-matrix=$(curl -s https://raw.githubusercontent.com/apple/swift-nio/main/scripts/generate_matrix.sh | bash)" >> "$GITHUB_OUTPUT"
env:
MATRIX_LINUX_5_9_ENABLED: false
MATRIX_LINUX_5_10_ENABLED: false
MATRIX_LINUX_COMMAND: "./dev/build-examples.sh"
MATRIX_LINUX_SETUP_COMMAND: "apt update && apt install -y protobuf-compiler && ./dev/build-examples.sh"

examples-matrix:
name: Examples
uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main
needs: construct-examples-matrix
uses: apple/swift-nio/.github/workflows/swift_test_matrix.yml@main
with:
name: "Examples"
matrix_linux_5_9_enabled: false
matrix_linux_5_10_enabled: false
matrix_linux_command: "apt update && apt install -y protobuf-compiler && ./dev/build-examples.sh"
matrix_string: '${{ needs.construct-examples-matrix.outputs.examples-matrix }}'

benchmarks:
name: Benchmarks
Expand Down
29 changes: 29 additions & 0 deletions Sources/GRPCCore/Status.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,32 @@ extension Status.Code {
/// operation.
public static let unauthenticated = Self(code: .unauthenticated)
}

extension Status {
/// Create a status from an HTTP status code for a response which didn't include a gRPC status.
///
/// - Parameter httpStatusCode: The HTTP status code to map to a status.
public init(httpStatusCode: Int) {
// See the "http-grpc-status-mapping.md" doc in grpc/grpc GitHub repo.
switch httpStatusCode {
case 400:
self = Status(code: .internalError, message: "HTTP 400: Bad Request")
case 401:
self = Status(code: .unauthenticated, message: "HTTP 401: Unauthorized")
case 403:
self = Status(code: .permissionDenied, message: "HTTP 403: Forbidden")
case 404:
self = Status(code: .unimplemented, message: "HTTP 404: Not Found")
case 429:
self = Status(code: .unavailable, message: "HTTP 429: Too Many Requests")
case 502:
self = Status(code: .unavailable, message: "HTTP 502: Bad Gateway")
case 503:
self = Status(code: .unavailable, message: "HTTP 503: Service Unavailable")
case 504:
self = Status(code: .unavailable, message: "HTTP 504: Gateway Timeout")
default:
self = Status(code: .unknown, message: "HTTP \(httpStatusCode)")
}
}
}
19 changes: 19 additions & 0 deletions Tests/GRPCCoreTests/StatusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ struct StatusTests {
func fitsInExistentialContainer() {
#expect(MemoryLayout<Status>.size <= 24)
}

@Test(
"From HTTP status code",
arguments: [
(400, Status(code: .internalError, message: "HTTP 400: Bad Request")),
(401, Status(code: .unauthenticated, message: "HTTP 401: Unauthorized")),
(403, Status(code: .permissionDenied, message: "HTTP 403: Forbidden")),
(404, Status(code: .unimplemented, message: "HTTP 404: Not Found")),
(429, Status(code: .unavailable, message: "HTTP 429: Too Many Requests")),
(502, Status(code: .unavailable, message: "HTTP 502: Bad Gateway")),
(503, Status(code: .unavailable, message: "HTTP 503: Service Unavailable")),
(504, Status(code: .unavailable, message: "HTTP 504: Gateway Timeout")),
(418, Status(code: .unknown, message: "HTTP 418")),
]
)
func convertFromHTTPStatusCode(code: Int, expected: Status) {
let status = Status(httpStatusCode: code)
#expect(status == expected)
}
}

0 comments on commit 97f7e97

Please sign in to comment.