Skip to content

Change CommandError and MessageInfo to public #199

@icanswiftabit

Description

@icanswiftabit

As a follow up to pull request #198

TL:DR;

The motivation behind it was the desire to react to such an error with a beautiful message.

Motivation

In our tool, we use SwiftNIO and ArgumentParser for handling slack commands. At one point a command can fail. For example when a user provides not expect a flag. This will produce CommandError. NIO's mapIfError will bring it to Error type. Hence at this moment, we can only print such error.

CommandError(commandStack: [SlackCommands.PR], parserError: ArgumentParser.ParserError.unknownOption(ArgumentParser.InputOrigin.Element.argumentIndex(0), ArgumentParser.Name.long("some-wrong-argument")))

Not very user friendly :(
But if CommandError and MessageInfo would be public. We could detect if the mapped error is a CommandError and use MessageInfo to print a much nicer message. Such as

Error: Missing expected argument '<project-name>'
Usage: Stats project <project-name> [--repository <repository>]
  See 'Stats project --help' for more information.

This is how we would use it

func handleWebHook(payload: SlackHookPayload) throws -> Future<String> {
    return commandDispatcher.dispatch(payload: payload).mapIfError { error -> String in
        guard let commandError = error as? ArgumentParser.CommandError, let commandType = commandError.commandStack.first.self else {
            return "Command failed with a message: \(error)"
        }
        return MessageInfo(error: commandError, type: commandType).fullText
    }
}

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