artusjs / core

Core package of Artus
MIT License
210 stars 16 forks source link

refactor(exception): exception filter need to handle inheritance of error clazz #255

Closed noahziheng closed 1 year ago

noahziheng commented 1 year ago

之前考虑通过 instanceof 匹配错误类的话,需要对 Filter 全量 Map 做遍历,有性能损耗,所以匹配时直接用了 Map().has(constructor) 的方案;

但这种情况下,对于以下的继承场景将有遗漏,如下:

class BaseError extends Error {}
class InputError extends BaseError {}

@Catch(BaseError)
class SomeFilter implements ExceptionFilterType {
    catch(err) {
        // 这里是拦截不到 InputError 的
    }
}

所以计划匹配阶段增加对异常实例原型链的循环,从子到父,直到 prototype 不为 class(根级),期间如有命中直接返回(相当于越精确的 Filter 优先级越高,如上例 @Catch(InputError) > @Catch(BaseError));

以此策略支持有继承场景的 ExceptionFilter,考虑到异常的继承嵌套不会很长(通常比 Filter 数量小且可控),性能损失相对有限。

codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 93.33% and project coverage change: -0.07 :warning:

Comparison is base (43649d0) 91.71% compared to head (a7bfe23) 91.65%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #255 +/- ## ========================================== - Coverage 91.71% 91.65% -0.07% ========================================== Files 49 49 Lines 1195 1198 +3 Branches 212 212 ========================================== + Hits 1096 1098 +2 - Misses 99 100 +1 ``` | [Impacted Files](https://app.codecov.io/gh/artusjs/core/pull/255?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=artusjs) | Coverage Δ | | |---|---|---| | [src/exception/utils.ts](https://app.codecov.io/gh/artusjs/core/pull/255?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=artusjs#diff-c3JjL2V4Y2VwdGlvbi91dGlscy50cw==) | `91.66% <93.33%> (-3.58%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.