AlariCode / nestjs-rmq

A custom library for NestJS microservice. It allows you to use RabbitMQ or AMQP.
https://purpleschool.ru
MIT License
285 stars 38 forks source link

Advanced Error Handling #6

Closed mjarmoc closed 4 years ago

mjarmoc commented 4 years ago

This PR introduces:

AlariCode commented 4 years ago

Thanks for PR, we will review it!

AlariCode commented 4 years ago

@mjarmoc, great PR! It will be useful to have more debugging info and ability to handle errors. But we have to keep some headers to stay consistent with .NetCore library that we use in production. Headers are:

-x-error
-x-status-code

We use -x as error marking header. So to keep naming convention, I suggest using names like:

-x-error-message -> -x-error
-x-error-code -> -x-status-code
-x-error-data -> -x-data
-x-error-service -> -x-service
-x-error-host -> -x-host
-x-error-type -> -x-type

Will it be ok?

And DEFAULT_ERROR_CODE should be deleted because it is no longer in use.

mjarmoc commented 4 years ago

@AlariCode sure thing, I will prepare the amendments during the weekend ;)

AlariCode commented 4 years ago

@mjarmoc, great, looking forward for it!

AlariCode commented 4 years ago

@mjarmoc great, I'll add some format and docs and merge it. But I think two types of error will lead us to code duplication. And we have default case in this code if no '-x-type' header exists:

export class RMQErrorHandler {
    public static handle(headers: any): any {
        switch (headers['-x-type']) {
            case ERROR_TYPE.TRANSPORT:
                return new RMQTransportError(
                    headers['-x-error'],
                    headers['-x-status-code'],
                    headers['-x-data'],
                    headers['-x-service'],
                    headers['-x-host']
                );
            case ERROR_TYPE.RMQ:
                return new RMQError(
                    headers['-x-error'],
                    headers['-x-status-code'],
                    headers['-x-data'],
                    headers['-x-service'],
                    headers['-x-host']
                );
            default:
                // ??
        }
    }
}

May be we can stick with one error class but with additional parameter like -x-type? Or we need to create 3rd type of error for default. What do you think?

AlariCode commented 4 years ago

@mjarmoc, I added docs, changed headers, user only one error class with multiple types and add more typing. Tested it and now ready to merge. Thanks for Merge Request!