How to throw an error inside an Umbraco Macro

This is a post about how to throw Umbraco Macro errors or how to turn off the generic error "Error loading Partial View script(....)" when loading Umbraco Macros in your Razor view.

In Umbraco 13, there is a MacroErrors property that defines how the errors inside Macro Partial Views are displayed and handeled. By default, the property is set to Inline to display a generic message that something went wrong with your file. The message reads: "Error loading Partial View script". I'm upgrading an Umbraco instance for a client and needed to know what the actual error is but was banging my head trying to figure out why the error wouldn't display normally.

After some digging I found that throwing the stack trace error behavior is not standard and I need to configure the ability to throw the error in the appsettings.json file. To do this, open the appsettings.json file and update the Umbraco\CMS\MacroErrors node to include the MacroErrors property with the value Throw like so:

"Umbraco": {
  "CMS": {
    "Global": {
      "Id": "",
      "SanitizeTinyMce": true
    },
    "Content": {
      "AllowEditInvariantFromNonDefault": true,
      "ContentVersionCleanupPolicy": {
        "EnableCleanup": true
      },
      "MacroErrors": "Throw"
    },
    "Unattended": {
      "UpgradeUnattended": true
    },
    "Security": {
      "AllowConcurrentLogins": false
    }
  }
},

Everyone's Umbraco node will be slightly different, but the main thing to update here is the MacroErrors node to be set to Throw.

Out of the box Umbraco doesn't include MacroErrors in the appsettings.json file, so chances are you need to add this node yourself. I hope this saves someone some debugging time. Happy coding!