Skip to content

Commit

Permalink
Expose HTTP status code when throwing WebPushException (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
morki authored Apr 21, 2024
1 parent ae035ee commit e7a3608
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/main/kotlin/com/interaso/webpush/WebPush.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ public class WebPush(
* @param statusCode the status code received from the server
* @param body the response body received from the server (optional)
* @return the subscription state based on the provided status code
* @throws WebPushException if authentication failed (status code 401 or 403),
* if the service is unavailable (status code 502 or 503),
* or if an unexpected response is received
* @throws WebPushStatusException if authentication failed (status code 401 or 403),
* if the service is unavailable (status code 502 or 503),
* or if an unexpected response is received
*/
public fun getSubscriptionState(statusCode: Int, body: String? = null): SubscriptionState {
return when (statusCode) {
200, 201, 202 -> SubscriptionState.ACTIVE
404, 410 -> SubscriptionState.EXPIRED
401, 403 -> throw WebPushException("Authentication failed: [$statusCode] - $body")
502, 503 -> throw WebPushException("Service unavailable: [$statusCode] - $body")
else -> throw WebPushException("Unexpected response: [$statusCode] - $body")
401, 403 -> throw WebPushStatusException(statusCode, "Authentication failed: [$statusCode] - $body")
502, 503 -> throw WebPushStatusException(statusCode, "Service unavailable: [$statusCode] - $body")
else -> throw WebPushStatusException(statusCode, "Unexpected response: [$statusCode] - $body")
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/main/kotlin/com/interaso/webpush/WebPushException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ package com.interaso.webpush
* @param message The detail message of the exception.
* @param cause The cause of the exception, or null if the cause is nonexistent or unknown.
*/
public class WebPushException(message: String, cause: Throwable? = null) : Exception(message, cause)
public sealed class WebPushException(
message: String,
cause: Throwable? = null,
) : Exception(message, cause)

/**
* Represents an exception that occurs during web push sending.
*
* @param statusCode The HTTP status code returned by server
* @param message The detail message of the exception.
* @param cause The cause of the exception, or null if the cause is nonexistent or unknown.
*/
public class WebPushStatusException(
public val statusCode: Int,
message: String,
cause: Throwable? = null,
) : WebPushException(message, cause)
6 changes: 4 additions & 2 deletions src/main/kotlin/com/interaso/webpush/WebPushService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class WebPushService(
* @param urgency The urgency level of the push notification (optional).
*
* @return current state of this subscription
* @throws WebPushException if an unexpected status code is received from the push service.
* @throws WebPushStatusException if an unexpected status code is received from the push service.
* @throws WebPushException if an unexpected exception is caught while constructing request.
*/
public fun send(
payload: String,
Expand All @@ -55,7 +56,8 @@ public class WebPushService(
* @param urgency The urgency level of the push notification (optional).
*
* @return current state of this subscription
* @throws WebPushException if an unexpected status code is received from the push service.
* @throws WebPushStatusException if an unexpected status code is received from the push service.
* @throws WebPushException if an unexpected exception is caught while constructing request.
*/
public fun send(
payload: ByteArray,
Expand Down

0 comments on commit e7a3608

Please sign in to comment.