hashicorp / go-multierror

A Go (golang) package for representing a list of errors as a single error.
Mozilla Public License 2.0
2.3k stars 123 forks source link

Implement sort interface for error messages #18

Closed bflad closed 6 years ago

bflad commented 6 years ago

When implementing a large amount of messaging, it can be very helpful to be able to sort the errors before printing them out.

Previously with errors appended from looping through a map, it can return something like the following:

11 error(s) occurred:

        * expected SNS topic attribute "ApplicationSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "HTTPSuccessFeedbackSampleRate" to match "^80$", received: ""
        * expected SNS topic attribute "LambdaSuccessFeedbackSampleRate" to match "^90$", received: ""
        * expected SNS topic attribute "LambdaSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "SQSFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "ApplicationSuccessFeedbackSampleRate" to match "^100$", received: ""
        * expected SNS topic attribute "ApplicationFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "HTTPSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "HTTPFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "LambdaFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "SQSSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""

Now it should be possible to sort.Sort(errors) before formatting so it returns:

11 error(s) occurred:

        * expected SNS topic attribute "ApplicationFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "ApplicationSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "ApplicationSuccessFeedbackSampleRate" to match "^100$", received: ""
        * expected SNS topic attribute "HTTPFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "HTTPSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "HTTPSuccessFeedbackSampleRate" to match "^80$", received: ""
        * expected SNS topic attribute "LambdaFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "LambdaSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "LambdaSuccessFeedbackSampleRate" to match "^90$", received: ""
        * expected SNS topic attribute "SQSFailureFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""
        * expected SNS topic attribute "SQSSuccessFeedbackRoleArn" to match "^arn:aws:iam::[0-9]{12}:role/sns-delivery-status-role-", received: ""

We could take this further and implement a SortedListFormatFunc or augment the existing ListFormatFunc to automatically sort as well.

mitchellh commented 6 years ago

This seems reasonable to me, especially since its up to the caller to decide whether they want to sort or not so it doesn't introduce any breaking changes.