apple / apple/swift-argument-parser
Return codes for custom error types
- Dominant language
- Swift
- Stars
- 3.8k
- Forks
- 411
- Avg merge
- 7d 13h
- Merged PRs (30d)
- 17
Description
I have a custom error type that I'm throwing from my command:
```
enum Error: LocalizedError {
case foo
...
var localizedDescription: String {
switch self {
case .foo: return "foo error"
...
}
}
}
```
These all surface with code 1 in the shell. I'd like at least some of them to report specific codes so I can more easily respond to them.
I've seen there's `ExitCode` which I could throw instead but then I don't have a single `Error` tracking all error states anymore and I'd need to make the error printing local to the throwing location because ExitCode does not do any error logging:
```
print("error bar")
throw ExitCode(2)
```
Currently I can just throw and tweak all the error messages in my `Error`'s `localizedDescription`.
I tried making my `Error` adopt `RawRepresentable` in the hopes it might pick up the raw values from there but it didn't.
Is there another way to attach specific codes to custom error types? Would it be possible to pick up error codes from `RawRepresentable` or some other error code protocol?
Contributor guide
Research direction
Start with the command's custom Error/LocalizedError path and the existing ExitCode entry point; trace how thrown errors become shell code 1. Done means custom error cases can expose distinct shell return codes without losing centralized error printing, with coverage for the requested behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100