Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 12, 2024
1 parent 4d8fe24 commit f6e2e71
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
16 changes: 14 additions & 2 deletions Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension EnumeratorMacroType: MemberMacro {
)
let segments = Array(syntax.segments)
let segmentIdx = parserError.context.lineNumber - 2
if segmentIdx < segments.count {
if segmentIdx >= 0, segmentIdx < segments.count {
let syntaxAtErrorLine = segments[segmentIdx]
errorSyntax = syntaxAtErrorLine
} else {
Expand All @@ -88,7 +88,19 @@ extension EnumeratorMacroType: MemberMacro {
let decls = SourceFileSyntax(
stringLiteral: rendered
).statements.compactMap { statement in
DeclSyntax(statement.item)
if let syntax = DeclSyntax(statement.item) {
return syntax
} else {
context.diagnose(
Diagnostic(
node: syntax,
message: MacroError.internalError(
"Could not convert an CodeBlockItemSyntax to a DeclSyntax"
)
)
)
return nil
}
}
if let withError = decls.first(where: \.hasError) {
context.diagnose(
Expand Down
3 changes: 3 additions & 0 deletions Sources/EnumeratorMacroImpl/MacroError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum MacroError: Error, CustomStringConvertible {
case renderedSyntaxContainsErrors(String)
case couldNotFindLocationOfNode(syntax: String)
case mustacheTemplateError(message: String)
case internalError(String)

var description: String {
switch self {
Expand All @@ -29,6 +30,8 @@ enum MacroError: Error, CustomStringConvertible {
"Could not find location of node for syntax:\n\(syntax)"
case let .mustacheTemplateError(message):
"Error while rendering the template: \(message)"
case let .internalError(message):
"An internal error occurred. Please file a bug report at https://github.com/mahdibm/enumerator-macro. Error:\n\(message)"
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Sources/EnumeratorMacroImpl/Types/EnumCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ struct EnumCase {
})
}
}

4 changes: 0 additions & 4 deletions Sources/EnumeratorMacroImpl/Types/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,4 @@ extension Optional: OptionalProtocol {
func asConvertedOptionalAny() -> Optional<Any> {
self.map { convertToCustomTypesIfPossible($0) }
}

func requireConvertedWrappedType() -> any Any.Type {
type(of: convertToCustomTypesIfPossible(self!))
}
}

0 comments on commit f6e2e71

Please sign in to comment.