Skip to content

Commit 321dbcc

Browse files
committed
Don't include source location for conversion errors
Currently when we encounter an error while writing RenderNode to disk, we include the source URL of the content that produced that RenderNode in the diagnostic. This turns the diagnostic into an inline diagnostic that we use for user-facing errors and warnings which is confusing. This resolves that issue and updates the diagnostic creation API in DocumentationConverter to make it less easy to make this mistake in the future.
1 parent 6c27a9e commit 321dbcc

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

Sources/SwiftDocC/Infrastructure/DocumentationConverter.swift

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
311311
}
312312
}
313313
} catch {
314-
results.append(.init(description: error.localizedDescription, source: source))
315-
diagnosticEngine.emit(.init(description: error.localizedDescription, source: source))
314+
recordProblem(from: error, in: &results, withIdentifier: "render-node")
316315
}
317316
}
318317
}
@@ -327,8 +326,7 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
327326
do {
328327
try outputConsumer.consume(assetsInBundle: bundle)
329328
} catch {
330-
conversionProblems.append(.init(description: error.localizedDescription, source: nil))
331-
diagnosticEngine.emit(.init(description: error.localizedDescription, source: nil))
329+
recordProblem(from: error, in: &conversionProblems, withIdentifier: "assets")
332330
}
333331

334332
// Write various metadata
@@ -338,17 +336,15 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
338336
try outputConsumer.consume(indexingRecords: indexingRecords)
339337
try outputConsumer.consume(assets: assets)
340338
} catch {
341-
conversionProblems.append(.init(description: error.localizedDescription, source: nil))
342-
diagnosticEngine.emit(.init(description: error.localizedDescription, source: nil))
339+
recordProblem(from: error, in: &conversionProblems, withIdentifier: "metadata")
343340
}
344341
}
345342

346343
if emitDigest {
347344
do {
348345
try outputConsumer.consume(problems: context.problems + conversionProblems)
349346
} catch {
350-
conversionProblems.append(.init(description: error.localizedDescription, source: nil))
351-
diagnosticEngine.emit(.init(description: error.localizedDescription, source: nil))
347+
recordProblem(from: error, in: &conversionProblems, withIdentifier: "problems")
352348
}
353349
}
354350

@@ -357,9 +353,7 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
357353
do {
358354
try outputConsumer.consume(documentationCoverageInfo: coverageInfo)
359355
} catch {
360-
let problem = Problem(description: error.localizedDescription, source: nil)
361-
conversionProblems.append(problem)
362-
diagnosticEngine.emit(problem)
356+
recordProblem(from: error, in: &conversionProblems, withIdentifier: "coverage")
363357
}
364358
case .none:
365359
break
@@ -412,6 +406,34 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
412406
return isDocumentPathToConvert || isExternalIDToConvert
413407
}
414408

409+
/// Record a problem from the given error in the given problem array.
410+
///
411+
/// Creates a ``Problem`` from the given `Error` and identifier, emits it to the
412+
/// ``DocumentationConverter``'s ``DiagnosticEngine``, and appends it to the given
413+
/// problem array.
414+
///
415+
/// - Parameters:
416+
/// - error: The error that describes the problem.
417+
/// - problems: The array that the created problem should be appended to.
418+
/// - identifier: A unique identifier the problem.
419+
private func recordProblem(
420+
from error: Swift.Error,
421+
in problems: inout [Problem],
422+
withIdentifier identifier: String
423+
) {
424+
let singleDiagnostic = Diagnostic(
425+
source: nil,
426+
severity: .error,
427+
range: nil,
428+
identifier: "org.swift.docc.documentation-converter.\(identifier)",
429+
summary: error.localizedDescription
430+
)
431+
let problem = Problem(diagnostic: singleDiagnostic, possibleSolutions: [])
432+
433+
diagnosticEngine.emit(problem)
434+
problems.append(problem)
435+
}
436+
415437
enum Error: DescribedError {
416438
case doesNotContainBundle(url: URL)
417439

@@ -427,21 +449,3 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
427449
}
428450
}
429451
}
430-
431-
private extension Problem {
432-
/// Creates a new problem with the given description and documentation source location.
433-
///
434-
/// Use this to provide a user-friendly description of an error,
435-
/// along with a direct reference to the source file and line number where you call this initializer.
436-
///
437-
/// - Parameters:
438-
/// - description: A brief description of the problem.
439-
/// - source: The URL for the documentation file that caused this problem, if any.
440-
/// - file: The source file where you call this initializer.
441-
init(description: String, source: URL?, file: String = #file) {
442-
let fileName = URL(fileURLWithPath: file).deletingPathExtension().lastPathComponent
443-
444-
let singleDiagnostic = Diagnostic(source: source, severity: .error, range: nil, identifier: "org.swift.docc.\(fileName)", summary: description)
445-
self.init(diagnostic: singleDiagnostic, possibleSolutions: [])
446-
}
447-
}

0 commit comments

Comments
 (0)