Added an exception handler to the check_token endpoint

Returns error in json when requested

[Fixes #46310153]