A malformed Jinja SQL template on a query-backed datasource makes
security_manager.raise_for_access() raise a raw jinja2 TemplateError via
process_jinja_sql. This propagated unwrapped through check_chart_access in the
explore permalink create/get commands and out of the API handlers, surfacing as
an opaque 500 instead of a proper 4xx.
Wrap the check_chart_access call in both commands to convert TemplateError into
a SupersetTemplateException (422), and handle that exception in the permalink API
post()/get() handlers.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>