Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Sources/FalClient/Realtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum FalRealtimeError: Error {
case unauthorized
case invalidInput
case invalidResult(requestId: String? = nil, causedBy: Error? = nil)
case serviceError(type: String, reason: String)
case serviceError(type: String, reason: String, requestId: String? = nil)
}

extension FalRealtimeError: LocalizedError {
Expand All @@ -35,11 +35,24 @@ extension FalRealtimeError: LocalizedError {
return NSLocalizedString("Invalid input format", comment: "FalRealtimeError.invalidInput")
case .invalidResult:
return NSLocalizedString("Invalid result", comment: "FalRealtimeError.invalidResult")
case let .serviceError(type, reason):
case let .serviceError(type, reason, _):
return NSLocalizedString("\(type): \(reason)", comment: "FalRealtimeError.serviceError")
}
}
}
public extension FalRealtimeError {
var requestId: String? {
switch self {
case let .invalidResult(requestId, _):
return requestId
case let .serviceError(_, _, requestId):
return requestId
default:
return nil
}
}
}


typealias SendFunction = (URLSessionWebSocketTask.Message) throws -> Void
typealias CloseFunction = () -> Void
Expand Down Expand Up @@ -355,7 +368,8 @@ func getError(_ message: Payload) -> FalRealtimeError? {
// not trigger the onError callback of the client
error != "TIMEOUT"
{
return FalRealtimeError.serviceError(type: error, reason: reason)
let requestId = message["request_id"].stringValue ?? message["requestId"].stringValue
return FalRealtimeError.serviceError(type: error, reason: reason, requestId: requestId)
}
return nil
}
Expand Down Expand Up @@ -485,3 +499,4 @@ public extension Realtime {
)
}
}

44 changes: 44 additions & 0 deletions Tests/FalClientTests/RealtimeSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@testable import FalClient
import Nimble
import Quick

class RealtimeSpec: QuickSpec {
override class func spec() {
describe("Realtime error decoding") {
it("attaches the request id to service errors") {
let message: Payload = [
"type": "x-fal-error",
"error": "MODEL_ERROR",
"reason": "failed to process",
"request_id": "req-123"
]

let error = getError(message)
guard case let .serviceError(_, _, requestId)? = error else {
fail("Expected serviceError with request identifier")
return
}

expect(requestId).to(equal("req-123"))
expect(error?.requestId).to(equal("req-123"))
}

it("leaves the request id unset when unavailable") {
let message: Payload = [
"type": "x-fal-error",
"error": "MODEL_ERROR",
"reason": "failed to process"
]

let error = getError(message)
guard case let .serviceError(_, _, requestId)? = error else {
fail("Expected serviceError when decoding realtime message")
return
}

expect(requestId).to(beNil())
expect(error?.requestId).to(beNil())
}
}
}
}