Skip to content

Commit 9ea171d

Browse files
committed
refactor: change error response
1 parent dd5e321 commit 9ea171d

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

Sources/ErrorMiddleware/ErrorMiddleware.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ public extension ErrorMiddleware {
4848
let reason: String
4949
let headers: HTTPHeaders
5050
let identifier: String?
51-
let status: String
51+
let status: Int
5252
let code: String
5353
switch error {
5454
case let appError as AppError:
5555
reason = appError.reason
5656
httpStatus = appError.status
5757
headers = appError.headers
5858
identifier = appError.identifier
59-
status = appError.status.code.description
59+
status = Int(appError.status.code)
6060
code = "\(appError.status.code.description).\(number).\(appError.number)"
6161
case let abort as AbortError:
6262
/// This is an abort error, we should use its status, reason, and headers
6363
reason = abort.reason
6464
httpStatus = abort.status
6565
headers = abort.headers
6666
identifier = abort.status.code.description
67-
status = "\(abort.status.code)"
67+
status = Int(abort.status.code)
6868
code = "\(abort.status.code).\(number).\(abort.number)"
6969
case let error as LocalizedError where !environment.isRelease:
7070
/// If not release mode, and error is debuggable, provide debug
@@ -73,17 +73,17 @@ public extension ErrorMiddleware {
7373
httpStatus = .internalServerError
7474
headers = [:]
7575
identifier = error.errorDescription
76-
status = "500"
77-
code = "500.\(number).0000"
76+
status = Int(HTTPStatus.internalServerError.code)
77+
code = "\(HTTPStatus.internalServerError.code).\(number).0000"
7878
default:
7979
/// Not an abort error, and not debuggable or in dev mode
8080
/// Just deliver a generic 500 to avoid exposing any sensitive error info
8181
reason = "Something went wrong."
8282
httpStatus = .internalServerError
8383
headers = [:]
8484
identifier = "something_went_wrong"
85-
status = "500"
86-
code = "500.\(number).0000"
85+
status = Int(HTTPStatus.internalServerError.code)
86+
code = "\(HTTPStatus.internalServerError.code).\(number).0000"
8787
}
8888
/// Report the error to logger.
8989
req.logger.report(error: error)

Sources/ErrorMiddleware/Responses/ErrorResponse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct ErrorResponse: Content {
3939
public var error: String?
4040
/// An optional status representing the error state.
4141
/// This field may contain a status code, textual representation, or other status-related information.
42-
public var status: String?
42+
public var status: Int?
4343
/// An optional code representing the error.
4444
/// This is often used to categorize errors into distinct types or classes, typically defined by the API.
4545
public var code: String?
@@ -61,7 +61,7 @@ public struct ErrorResponse: Content {
6161
isError: Bool = true,
6262
reason: String,
6363
error: String? = nil,
64-
status: String? = nil,
64+
status: Int? = nil,
6565
code: String? = nil,
6666
errorUri: String = "https://example.com/doc/errors"
6767
) {

Tests/ErrorMiddlewareTests/ErrorMiddlewareTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct ErrorMiddlewareTests {
4444
#expect(error.isError == true)
4545
#expect(error.reason == "Not Found")
4646
#expect(error.error == "404")
47-
#expect(error.status == "404")
47+
#expect(error.status == 404)
4848
#expect(error.code == "404.1.")
4949
#expect(error.errorUri == "https://example.com/doc/errors")
5050
}
@@ -61,7 +61,7 @@ struct ErrorMiddlewareTests {
6161
#expect(error.isError == true)
6262
#expect(error.reason == "Not Found")
6363
#expect(error.error == "404")
64-
#expect(error.status == "404")
64+
#expect(error.status == 404)
6565
#expect(error.code == "404.1.")
6666
#expect(error.errorUri == "https://example.com/doc/errors")
6767
}

Tests/ErrorMiddlewareTests/ErrorResponseTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ struct ErrorResponseTests {
3434
isError: true,
3535
reason: "reason of error",
3636
error: "error",
37-
status: "404",
37+
status: 404,
3838
code: "404.1.0000",
3939
errorUri: "error uri"
4040
)
4141
var secondErrorResponse = ErrorResponse(
4242
isError: true,
4343
reason: "reason of error",
4444
error: "error",
45-
status: "404",
45+
status: 404,
4646
code: "404.1.0000",
4747
errorUri: "error uri"
4848
)

0 commit comments

Comments
 (0)