Skip to content

Commit

Permalink
Add error messages to errors thrown by Hummingbird
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Apr 30, 2024
1 parent ec9d486 commit 6bfd41f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Sources/Hummingbird/Files/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ public struct FileMiddleware<Context: BaseRequestContext, Provider: FileProvider

// Remove percent encoding from URI path
guard let path = request.uri.path.removingPercentEncoding else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "Invalid URL")
}

// file paths that contain ".." are considered illegal
guard !path.contains("..") else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "URLs including '..' are not supported")
}

let fullPath = self.fileProvider.getFullPath(path)
Expand Down Expand Up @@ -221,7 +221,7 @@ extension FileMiddleware {

if let rangeHeader = request.headers[.range] {
guard let range = getRangeFromHeaderValue(rangeHeader) else {
throw HTTPError(.rangeNotSatisfiable)
throw HTTPError(.rangeNotSatisfiable, message: "Unable to read range requested from file")
}
// range request conditional on etag or modified date being equal to value in if-range
if let ifRange = request.headers[.ifRange], ifRange != headers[.eTag], ifRange != headers[.lastModified] {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Router/Parameters+UUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Parameters {
guard let param = self[s[...]],
let result = UUID(uuidString: String(param))
else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "Parameter '\(s)' can not be converted to the expected type (UUID)")
}
return result
}
Expand All @@ -53,7 +53,7 @@ extension Parameters {
public func requireAll(_ s: String, as: UUID.Type) throws -> [UUID] {
return try self[values: s[...]].map {
guard let result = UUID(uuidString: String($0)) else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "One of the parameters '\(s)' can not be converted to the expected type (UUID)")
}
return result
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Hummingbird/Router/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public extension Parameters {
/// - Parameter s: parameter id
func require(_ s: String) throws -> String {
guard let param = self[s[...]].map({ String($0) }) else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "Expected parameter '\(s)' does not exist")
}
return param
}
Expand All @@ -59,7 +59,7 @@ public extension Parameters {
guard let param = self[s[...]],
let result = T(String(param))
else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "Parameter '\(s)' can not be converted to the expected type (\(T.self))")
}
return result
}
Expand All @@ -72,7 +72,7 @@ public extension Parameters {
guard let param = self[s[...]],
let result = T(rawValue: String(param))
else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "Parameter '\(s)' can not be converted to the expected type (\(T.self))")
}
return result
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public extension Parameters {
func requireAll<T: LosslessStringConvertible>(_ s: String, as: T.Type) throws -> [T] {
return try self[values: s[...]].map {
guard let result = T(String($0)) else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "One of the parameters '\(s)' can not be converted to the expected type (\(T.self))")
}
return result
}
Expand All @@ -120,7 +120,7 @@ public extension Parameters {
func requireAll<T: RawRepresentable>(_ s: String, as: T.Type) throws -> [T] where T.RawValue == String {
return try self[values: s[...]].map {
guard let result = T(rawValue: String($0)) else {
throw HTTPError(.badRequest)
throw HTTPError(.badRequest, message: "One of the parameters '\(s)' can not be converted to the expected type (\(T.self))")
}
return result
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdHTTP2/HTTP2Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct HTTP2UpgradeChannel: HTTPChannelHandler {
public init(
tlsConfiguration: TLSConfiguration,
additionalChannelHandlers: @escaping @Sendable () -> [any RemovableChannelHandler] = { [] },
responder: @escaping @Sendable (Request, Channel) async throws -> Response = { _, _ in throw HTTPError(.notImplemented) }
responder: @escaping @Sendable (Request, Channel) async throws -> Response = { _, _ in throw HTTPError(.notFound) }
) throws {
var tlsConfiguration = tlsConfiguration
tlsConfiguration.applicationProtocols = NIOHTTP2SupportedALPNProtocols
Expand Down

0 comments on commit 6bfd41f

Please sign in to comment.