Skip to content

Commit

Permalink
Improve error message for missing reflection data (#2038)
Browse files Browse the repository at this point in the history
Motivation:

The message for missing reflection data isn't very helpful as it doesn't
indicate what reflection data is missing.

Modifications:

- Improve the error message
- Add note about well-known-types

Result:

Clearer error message
  • Loading branch information
glbrntt authored Sep 2, 2024
1 parent 3713967 commit d13926b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
18 changes: 12 additions & 6 deletions Sources/GRPCReflectionService/Server/ReflectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public final class ReflectionService: CallHandlerProvider, Sendable {
/// - Parameter fileURLs: The URLs of the files containing serialized reflection data.
/// - Parameter version: The version of the reflection service to create.
///
/// - Note: Reflection data for well-known-types must be provided if any of your reflection data depends
/// on them.
/// - Throws: When a file can't be read from disk or parsed.
public convenience init(reflectionDataFileURLs fileURLs: [URL], version: Version) throws {
let filePaths: [String]
Expand All @@ -64,6 +66,8 @@ public final class ReflectionService: CallHandlerProvider, Sendable {
/// - Parameter filePaths: The paths to files containing serialized reflection data.
/// - Parameter version: The version of the reflection service to create.
///
/// - Note: Reflection data for well-known-types must be provided if any of your reflection data depends
/// on them.
/// - Throws: When a file can't be read from disk or parsed.
public init(reflectionDataFilePaths filePaths: [String], version: Version) throws {
let fileDescriptorProtos = try ReflectionService.readSerializedFileDescriptorProtos(
Expand Down Expand Up @@ -218,12 +222,14 @@ internal struct ReflectionServiceData: Sendable {
let serializedFileDescriptorProto = protoData.serializedFileDescriptorProto
serializedFileDescriptorProtos.append(serializedFileDescriptorProto)
} else {
return .failure(
GRPCStatus(
code: .notFound,
message: "The provided file or a dependency of the provided file could not be found."
)
)
let base = "No reflection data for '\(currentFileName)'"
let message: String
if fileName == currentFileName {
message = base + "."
} else {
message = base + " which is a dependency of '\(fileName)'."
}
return .failure(GRPCStatus(code: .notFound, message: message))
}
visited.insert(currentFileName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ final class ReflectionServiceIntegrationTests: GRPCTestCase {
XCTAssertEqual(message.errorResponse.errorCode, Int32(GRPCStatus.Code.notFound.rawValue))
XCTAssertEqual(
message.errorResponse.errorMessage,
"The provided file or a dependency of the provided file could not be found."
"No reflection data for 'invalidFileName.proto'."
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ final class ReflectionServiceUnitTests: GRPCTestCase {
status,
GRPCStatus(
code: .notFound,
message: "The provided file or a dependency of the provided file could not be found."
message: "No reflection data for 'invalid.proto'."
)
)
}
Expand All @@ -368,7 +368,8 @@ final class ReflectionServiceUnitTests: GRPCTestCase {
status,
GRPCStatus(
code: .notFound,
message: "The provided file or a dependency of the provided file could not be found."
message:
"No reflection data for 'invalidDependency' which is a dependency of 'bar1.proto'."
)
)
}
Expand Down

0 comments on commit d13926b

Please sign in to comment.