Skip to content

Renamed HTTPError to PushServiceError #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 24, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A server-side Swift implementation of the WebPush standard.
## Quick Links

- [Documentation](https://swiftpackageindex.com/mochidev/swift-webpush/documentation)
- [Symbol Exploration](https://swiftinit.org/docs/mochidev.swift-webpush)
- [Updates on Mastodon](https://mastodon.social/tags/SwiftWebPush)

## Installation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// HTTPError.swift
// PushServiceError.swift
// swift-webpush
//
// Created by Dimitri Bouniol on 2024-12-13.
Expand All @@ -9,12 +9,12 @@
import AsyncHTTPClient
import Foundation

/// An unknown HTTP error was encountered.
/// An unknown Push Service error was encountered.
///
/// - SeeAlso: [RFC 8030 Generic Event Delivery Using HTTP Push](https://datatracker.ietf.org/doc/html/rfc8030)
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push](https://datatracker.ietf.org/doc/html/rfc8292)
/// - SeeAlso: [Sending web push notifications in web apps and browsers — Review responses for push notification errors](https://developer.apple.com/documentation/usernotifications/sending-web-push-notifications-in-web-apps-and-browsers#Review-responses-for-push-notification-errors)
public struct HTTPError: LocalizedError, Sendable {
public struct PushServiceError: LocalizedError, Sendable {
/// The HTTP response that was returned from the push service..
public let response: HTTPClientResponse

Expand All @@ -28,11 +28,11 @@ public struct HTTPError: LocalizedError, Sendable {
}

public var errorDescription: String? {
"A \(response.status) HTTP error was encountered: \(capturedResponseDescription)."
"A \(response.status) Push Service error was encountered: \(capturedResponseDescription)."
}
}

extension HTTPError: Hashable {
extension PushServiceError: Hashable {
public static func == (lhs: Self, rhs: Self) -> Bool {
"\(lhs.capturedResponseDescription)" == "\(rhs.capturedResponseDescription)"
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/WebPush/WebPushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public actor WebPushManager: Sendable {
logger.error("The encrypted payload was too large and was rejected by the push service.")
throw MessageTooLargeError()
// TODO: 429 too many requests, 500 internal server error, 503 server shutting down - check config and perform a retry after a delay?
default: throw HTTPError(response: response)
default: throw PushServiceError(response: response)
}
logger.trace("Successfully sent notification")
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/WebPushTests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import Testing
#expect("\(Base64URLDecodingError().localizedDescription)" == "The Base64 data could not be decoded.")
}

@Test func httpError() {
@Test func pushServiceError() {
let response = HTTPClientResponse(status: .notFound)
#expect(HTTPError(response: response) == HTTPError(response: response))
#expect(HTTPError(response: response).hashValue == HTTPError(response: response).hashValue)
#expect(HTTPError(response: response) != HTTPError(response: HTTPClientResponse(status: .internalServerError)))
#expect("\(HTTPError(response: response).localizedDescription)" == "A 404 Not Found HTTP error was encountered: \(response).")
#expect(PushServiceError(response: response) == PushServiceError(response: response))
#expect(PushServiceError(response: response).hashValue == PushServiceError(response: response).hashValue)
#expect(PushServiceError(response: response) != PushServiceError(response: HTTPClientResponse(status: .internalServerError)))
#expect("\(PushServiceError(response: response).localizedDescription)" == "A 404 Not Found Push Service error was encountered: \(response).")
}

@Test func messageTooLargeError() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/WebPushTests/WebPushManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ struct WebPushManagerTests {
}))
)

await #expect(throws: HTTPError.self) {
await #expect(throws: PushServiceError.self) {
try await manager.send(string: "hello", to: .mockedSubscriber())
}
}
Expand Down
Loading