Skip to content

Commit 9109c6c

Browse files
author
Catalina Turlea
committed
Added ServerErrorWithCode and updated comments
1 parent b231246 commit 9109c6c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Sources/Base/OAuth2Error.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ public enum OAuth2Error: Error, CustomStringConvertible, Equatable {
8282
/// There is no delegate associated with the password grant flow instance.
8383
case noPasswordGrantDelegate
8484

85-
case clientError(Int)
85+
/// Generic client error 400<..500
86+
case clientErrorWithStatus(Int)
8687

88+
/// Generic server error <=501
89+
case serverErrorWithStatus(Int)
8790

8891
// MARK: - Request errors
8992

@@ -275,7 +278,9 @@ public enum OAuth2Error: Error, CustomStringConvertible, Equatable {
275278
return message ?? "The requested scope is invalid, unknown, or malformed."
276279
case .serverError:
277280
return "The authorization server encountered an unexpected condition that prevented it from fulfilling the request."
278-
case .clientError(let statusCode):
281+
case .serverErrorWithStatus(let statusCode):
282+
return "The authorization server encountered an unexpected condition, returning \(statusCode)"
283+
case .clientErrorWithStatus(let statusCode):
279284
return "The authorization server returned \(statusCode)"
280285
case .temporarilyUnavailable(let message):
281286
return message ?? "The authorization server is currently unable to handle the request due to a temporary overloading or maintenance of the server."

Sources/Flows/OAuth2.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ open class OAuth2: OAuth2Base {
352352
/**
353353
If there is a refresh token, use it to receive a fresh access token.
354354

355-
Does not remove the refresh_token in case of a failure. For client errors (400..<500), the callback will provide the status code in the .clientError(Int)
355+
Does not remove the refresh_token in case of a failure. For client errors (4xx), the callback will provide the status code in the .clientErrorWithStatus(Int). For server errors (5xx), the callback provides the status code in .serverErrorWithStatus(Int)
356356

357357
- parameter params: Optional key/value pairs to pass during token refresh
358358
- parameter callback: The callback to call after the refresh token exchange has finished
@@ -366,11 +366,12 @@ open class OAuth2: OAuth2Base {
366366
do {
367367
let data = try response.responseData()
368368
let json = try self.parseRefreshTokenResponseData(data)
369-
switch response.response.statusCode {
370-
case 500:
371-
throw OAuth2Error.serverError
369+
let statusCode = response.response.statusCode
370+
switch statusCode {
372371
case 400..<500:
373-
throw OAuth2Error.clientError(response.response.statusCode)
372+
throw OAuth2Error.clientErrorWithStatus(statusCode)
373+
case 500...599:
374+
throw OAuth2Error.serverErrorWithStatus(statusCode)
374375
default:
375376
throw OAuth2Error.generic("Failed with status \(response.response.statusCode)")
376377
}

0 commit comments

Comments
 (0)