Skip to content

content:beforeSave cancellation returns an unstructured 500 instead of a safe API error #2197

Closed

Description

@kharl26

Summary

content:beforeSave is documented as allowing a plugin to cancel a save by throwing. The hook pipeline rethrows an abort-policy error, but EmDashRuntime.handleContentCreate() and handleContentUpdate() do not convert that expected cancellation into an ApiResult error.

The exception therefore escapes the content endpoint as an unstructured server error instead of a stable JSON response. The built-in admin now displays structured API errors in its save/autosave toasts, so the remaining blocker is the missing server-side rejection contract—not admin rendering.

Reproduction

  1. Register a plugin with a content:beforeSave hook that rejects a save.
  2. Create or update content through the REST API or admin.
  3. Observe that the save is cancelled, but the response is an unstructured 500 rather than EmDash's normal { success: false, error: { code, message } } envelope.

Required contract

Plugins need a documented way to reject a save with a message that is safe to expose to the editor. Unexpected exceptions must remain internal errors and must not leak stack traces or arbitrary exception messages.

This could be an exported rejection error/helper or another explicit hook result, but the distinction between an expected cancellation and a plugin crash must be part of the public plugin API.

Acceptance criteria

  • The plugin API exposes and documents a structured content:beforeSave rejection mechanism.
  • Trusted and sandboxed hooks can reject both content creation and update.
  • Expected rejections return a stable error code and 4xx status in the normal API envelope.
  • The safe plugin-provided rejection message reaches the admin's existing error toast.
  • Unexpected hook exceptions return a generic 500 response without exposing internal error details.
  • Tests cover create/update, trusted/sandboxed hooks, expected rejection messages, and unexpected exceptions.

Out of scope

  • Adding a new admin error component; the current save and autosave mutations already surface structured API error messages.
  • Changing content:afterSave error handling.

Activity

  1. added this to the 1.0 milestone on Aug 13, 2026
  2. changed the title [-]content:beforeSave rejections reach neither the API response nor the admin UI[/-] [+]content:beforeSave cancellation returns an unstructured 500 instead of a safe API error[/+] on Aug 16, 2026
  3. ascorbic commented on Aug 16, 2026

    @ascorbic
    Member

    Rescoped after revalidating against current main. The admin now parses structured API errors and displays save/autosave failures in error toasts, so the original admin-display problem was removed. The remaining verified blocker is server-side: an expected content:beforeSave cancellation still escapes as an unstructured 500. The issue now asks for a safe public rejection contract while keeping unexpected plugin exceptions generic.

  4. danielmlr commented on Aug 23, 2026

    @danielmlr
    Member

    I picked this up and opened a PR for the trusted-hook half (linked above in the cross-references). ContentSaveRejectedError becomes a structured SAVE_REJECTED / 422 with the plugin's message, and every other hook exception becomes a generic CONTENT_HOOK_ERROR response instead of an unstructured 500.

    Two findings from working on it, one of them with a design question.

    Sandboxed hooks cannot cancel a save at all. runSandboxedBeforeSave in emdash-runtime.ts wraps each hook in try { … } catch { console.error(…) } and continues, so a throwing sandboxed hook is logged and the save proceeds. The sandboxed half of the acceptance criteria therefore needs new transport, not an error conversion. invokeHook has no error envelope across the RPC boundary; invokeRoute has one, but SANDBOX_ROUTE_ERROR_DEFINITIONS is a closed enum with host-side fixed messages, and it deliberately never carries plugin-authored text.

    The question before I build that half: may a sandboxed plugin's rejection message cross the boundary to the editor, through a SAVE_REJECTED variant of the envelope with a free-text field? Or should sandboxed rejections stay message-less with a fixed host string, the way a sandboxed content:beforeDelete produces "Delete blocked by sandboxed plugin hook"? The trusted-path contract works with either answer. The envelope shape does not, so I would rather not guess. Happy to build the sandbox half once this is decided.

    Side finding, separate from this issue: media:beforeUpload has the same escape shape. The docs say "throw to cancel", the hook pipeline rethrows on abort policy, and the media route catches it with handleError, which by design replaces the message with a generic "Upload failed" / 500. So the cancellation works, but no plugin message can reach the client there either. I can file that separately if useful.

  5. virafb commented on Aug 27, 2026

    @virafb

    Confirming this from a production deployment, and adding a third code path with
    the same root cause that the issue does not currently cover.

    A duplicate of this exists, and it is ours

    We filed #2684 on 2026-08-26 describing the same defect, having missed this
    issue in our own prior-art search. Yours is the original. Ours carries a source
    walk-through of the errorPolicy: "abort" path and the documentation quotes, if
    any of that is worth merging across, but it should be closed in favour of this
    one rather than tracked separately.

    The publish path has the same problem, in a worse shape

    The acceptance criteria here cover creation and update. There is a third path,
    and on it the hook does not run at all.

    handleContentPublish contains no call to runContentBeforeSave. Checked in
    0.32.0, 0.34.0 and 0.35.0.

    That matters because publishing is a second way for content to reach the public.
    Restoring a revision and publishing it puts data live that the hook has never
    seen — written before the rule existed, or before the plugin was installed. So a
    content:beforeSave hook is not the guard it appears to be: it guards the save,
    and the save is not the only route to publication.

    Worth folding into this issue rather than filing separately, because the fix
    shape depends on the answer here. If the outcome is the structured rejection
    mechanism the acceptance criteria describe, the natural next question is whether
    publication is a point at which that mechanism should run.

    Worth noting alongside: #2300 asks for a pre-publish validation hook with
    full content context, for exactly this editorial case. That feature would close
    this gap properly. What we describe is the same hole from the defect side — a
    hook documented as guarding content does not run on the operation that makes
    content public.

    What it looked like in production

    On 2026-07-27 our production 5xx alarm fired twice and recovered twice inside
    nine minutes. Nothing was broken and no visitor was affected. An editor was
    saving a content entry with a required field left blank, our plugin's
    beforeSave hook threw to reject it — as documented — and each rejection was
    served as a 500.

    The editor was told the server had failed, for a mistake they could have fixed
    themselves. The alarm treated a working guard as an outage. That is the cost of
    the two cases being indistinguishable: not only a confusing message, but a page
    that wakes somebody up.

    One note from writing the regression tests

    We keep a characterization suite that drives the real content routes against a
    real database with a real plugin registered, and asserts the HTTP responses an
    editor's browser receives.

    A detail that may be worth knowing if tests are added for the acceptance criteria
    above. validateContentData — EmDash's own schema validation, working correctly
    — already returns VALIDATION_ERROR and already maps to 400. So an assertion
    on "status 400 with code VALIDATION_ERROR" passes against a completely
    unpatched install and proves nothing about the hook path. The discriminator has
    to be the plugin's own message.

    We got that wrong in the first version of the suite and caught it only by running
    it deliberately against an unpatched install. If the rejection mechanism lands
    with a distinct error code, that problem disappears — which is an argument for a
    distinct code rather than reusing VALIDATION_ERROR.

    Versions

    Observed on 0.32.0, which we run. Re-checked and present unchanged in 0.34.0
    and 0.35.0, the current latest. Also present in the deprecated 1.0.0
    published in April, which puts the gap at four months old at least.

    Happy to send a PR for the publish path if a shape would help, once the rejection
    contract this issue asks for is settled.

  6. danielmlr commented on Sep 2, 2026

    @danielmlr
    Member

    Thanks. The half you were waiting on is out, not just merged: #2617 shipped in emdash 0.36.0, now latest. Throw ContentSaveRejectedError and you get SAVE_REJECTED with a 422; anything else the hook throws comes back as CONTENT_HOOK_ERROR. That's your discriminator.

    Sandboxed hooks still can't cancel a save, so that half of this issue is still open.

    Your publish finding holds on main. One thing you didn't mention: publish doesn't just flip status. On collections with revisions it copies the staged draft columns onto the live row, slug included, inside repo.publish().

    On folding it in here, I don't agree. In a normal edit-then-publish the hook did see the field data. It never sees the slug, and never the rest of what #2300 wants. The real hole is content the hook never saw at all: a restored revision, or a scheduled publish firing later. That's a missing hook, not a badly shaped error.

    Which makes it a feature, and feature PRs here get closed without a maintainer's go-ahead on a Discussion. #1995 is that Discussion and it's had no reply since July. That's what's blocking your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions