Skip to content

Return codes for custom error types #230

@finestructure

Description

@finestructure

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions