Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve string interpolation of errors #6482

Merged
merged 4 commits into from Apr 26, 2023
Merged

Conversation

tomerd
Copy link
Member

@tomerd tomerd commented Apr 25, 2023

motivation: better error messages

changes:

  • audit usage of "(error)" and replace them with "(error.interpolationDescription)"
  • add ADPI to pass an optional underlying error to observability's debug, info and warn calls such that it automatically interpolates the error correctly
  • update call sites

rdar://108418554

…ion on errors

motivation: better error messages

changes:
* audit usage of "\(error)" and replace them with "\(error.interpolationDescription)"
* add ADPI to pass an optional underlying error to observability's debug, info and warn calls such that it automatically interpolates the error correctly
* update call sites

rdar://108418554
@tomerd tomerd changed the title use Error::interpolationDescription when performing string interpolat… use Error::interpolationDescription when performing string interpolation on errors Apr 25, 2023
@tomerd tomerd changed the title use Error::interpolationDescription when performing string interpolation on errors improve string interpolation of errors Apr 25, 2023
@tomerd
Copy link
Member Author

tomerd commented Apr 25, 2023

@swift-ci smoke test

@tomerd
Copy link
Member Author

tomerd commented Apr 25, 2023

@swift-ci smoke test windows

@neonichu
Copy link
Member

Could you call out the core changes? Looks like a look of formatting stuff which is difficult to wade through.

@@ -173,45 +174,69 @@ extension DiagnosticsEmitterProtocol {
self.emit(.init(severity: severity, message: message, metadata: metadata))
}

public func emit(error message: String, metadata: ObservabilityMetadata? = .none) {
public func emit(error message: String, metadata: ObservabilityMetadata? = .none, underlyingError: Error? = .none) {
let message = makeMessage(from: message, underlyingError: underlyingError)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change here

}

public func emit(info message: String, metadata: ObservabilityMetadata? = .none) {
public func emit(info message: String, metadata: ObservabilityMetadata? = .none, underlyingError: Error? = .none) {
let message = makeMessage(from: message, underlyingError: underlyingError)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change here

}

public func emit(debug message: String, metadata: ObservabilityMetadata? = .none) {
public func emit(debug message: String, metadata: ObservabilityMetadata? = .none, underlyingError: Error? = .none) {
let message = makeMessage(from: message, underlyingError: underlyingError)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change here

@@ -236,6 +261,14 @@ extension DiagnosticsEmitterProtocol {
return false
}
}

private func makeMessage(from message: String, underlyingError: Error?) -> String {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change here

} else {
message = "\(error)"
message = error.interpolationDescription
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change here

Copy link
Member

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only minor doc comment nit, otherwise LGTM

@MaxDesiatov
Copy link
Member

Long term I wonder if we could introduce a new ErrorMessage type conforming to ExpressibleByStringInterpolation which would add interpolationDescription automatically without a need to remember to use that manually?

Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Sources/Basics/Errors.swift Outdated Show resolved Hide resolved
@tomerd
Copy link
Member Author

tomerd commented Apr 26, 2023

@swift-ci smoke test

@tomerd
Copy link
Member Author

tomerd commented Apr 26, 2023

@swift-ci smoke test windows

@tomerd tomerd enabled auto-merge (squash) April 26, 2023 18:04
@tomerd
Copy link
Member Author

tomerd commented Apr 26, 2023

@swift-ci smoke test windows

@tomerd tomerd merged commit 0551596 into apple:main Apr 26, 2023
5 checks passed
tomerd added a commit to tomerd/swift-package-manager that referenced this pull request Apr 26, 2023
motivation: better error messages

changes:
* audit usage of "\(error)" and replace them with "\(error.interpolationDescription)"
* add ADPI to pass an optional underlying error to observability's debug, info and warn calls such that it automatically interpolates the error correctly
* update call sites

rdar://108418554
tomerd added a commit that referenced this pull request May 1, 2023
motivation: better error messages

changes:
* audit usage of "\(error)" and replace them with "\(error.interpolationDescription)"
* add ADPI to pass an optional underlying error to observability's debug, info and warn calls such that it automatically interpolates the error correctly
* update call sites

rdar://108418554
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants