Skip to content

Commit 067938b

Browse files
authored
Allow a Status to be created from an HTTP status code (#2170)
Motivation: gRPC has a well defined mapping of HTTP status codes to gRPC status codes for when a response doesn't explicitly include a gRPC status. Modifications: - Add an init to status to allow for a status to be created from an HTTP status code. Result: Can create a status from an HTTP status code
1 parent 0db5cc0 commit 067938b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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)