Search code examples
c#asp.net-coreerror-handlingasp.net-core-localization

Handling exceptions during localizing in .net5


I would like to ask you if it is possible to handle exception during localizing? For instance I have made recently mistake in my resx file, and my localizer caused that in one language users got error (in translated version I wrote something like "foo {2} bar" instead of "foo {0} bar". I would like to handle this exception that localizer returns the "not translated" string.

Is it possible to write some kind of special exception handler for localizer? Should it implements any interface or something?

Many thanks for your answer.


Solution

  • I think you might need to custom a Resource filters with OnResourceExecuted().

    Filter types

    Each filter type is executed at a different stage in the filter pipeline:

    • Authorization filters

    run first and are used to determine whether the user is authorized for the request. Authorization filters short-circuit the pipeline if the request is not authorized.

    • Resource filters

    Run after authorization.
    OnResourceExecuting runs code before the rest of the filter pipeline. For example, OnResourceExecuting runs code before model binding.
    OnResourceExecuted runs code after the rest of the pipeline has completed.

    • Action filters

    Run code immediately before and after an action method is called.
    Can change the arguments passed into an action. Can change the result returned from the action.
    Are not supported in Razor Pages.

    • Exception filters

    apply global policies to unhandled exceptions that occur before the response body has been written to.

    • Result filters

    run code immediately before and after the execution of action results. They run only when the action method has executed successfully. They are useful for logic that must surround view or formatter execution.

    The following diagram shows how filter types interact in the filter pipeline.

    enter image description here