Answers.Body: <p>This will do it, and you can change the 400 to whatever type of <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes" rel="nofollow">http error</a> you want:</p>
  
  <pre class="lang-twig prettyprint-override"><code>throw new HttpException(400, 'Error message text');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-25T17:35:18.047Z
Answers.LastActivityDate: 2014-06-27T16:25:09.090Z
Answers.LastEditDate: 2014-06-27T16:25:09.090Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p>Craft itself (I'm referencing the EntriesController) checks to see if an entry is saved. If it doesn't it, points back to the template and hands it an error message in the session and any needed variables.</p>
  
  <p>In the case of your plugin, gather your form data and errors and send it back. Craft does it all in a single Model. You can do the same or different. It doesn't matter so long as those using the plugin knows what's up.</p>
  
  <p>Example based on a Craft Controller (with optional Ajax response):</p>
  
  <pre><code>if (craft()-&gt;request-&gt;isAjaxRequest())
  {
      $this-&gt;returnJson(array(
          'errors' =&gt; $errors,
      ));
  }
  else
  {
      $userSessionService-&gt;setError(Craft::t('Error Message!'));
  
      // Send the data back to the template.
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'data' =&gt; $data,
          'errors' =&gt; $errors,
      ));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-25T18:50:36.587Z
Answers.LastActivityDate: 2014-06-25T18:50:36.587Z
Answers.UserDisplayName: Bryan Redeagle
Answers.Body: <p>I'm not familiar with EE's show_error, but a plugin's error template logic is identical to the logic that Craft uses to determine what to display.</p>
  
  <p>From your plugin, if you:</p>
  
  <pre><code>throw new HttpException($statusCode, $optionalMessage)
  </code></pre>
  
  <p>Where $statusCode is 404 or 400, or any proper <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes" rel="noreferrer">http status code</a>, Craft will load the corresponding 404.html, 400.html, etc. from the craft/app/templates folder displaying the optional message, or the more generic message supplied in the template if no optional one is provided.</p>
  
  <p>However, if you have supplied your own 404, 400, etc. from your own craft/templates folder, Craft will display that one instead of the one it provides so you can customize these errors for your site.</p>
  
  <p>If you:</p>
  
  <pre><code>throw new Exception($optionalMessage)
  </code></pre>
  
  <p>while <a href="http://buildwithcraft.com/docs/config-settings#devMode" rel="noreferrer">devMode</a> is enabled, then Craft will load exception.html which will display the full error long with stack trace to help in debugging.  If devMode is not enabled, it will display the more generic error.html.</p>
  
  <p>Craft specifically checks for database connection errors and will display the error.html template while setting a 500 status code.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-25T19:29:15.583Z
Answers.LastActivityDate: 2014-06-25T20:38:11.790Z
Answers.LastEditDate: 2014-06-25T20:38:11.790Z
Answers.UserDisplayName: Brad Bell
Body: <p>Basically, I'm trying to emulate <code>show_error</code> from EE/CI: <a href="http://ellislab.com/codeigniter/user-guide/general/errors.html">http://ellislab.com/codeigniter/user-guide/general/errors.html</a></p>
  
  <p>I want to just use the generic Craft error template (<code>/craft/app/templates/error.html</code>), like what happens if you have a DB connection error, and have it serve a 500.</p>
  
  <p>This is from with a plugin controller.</p>
  
  <p>What's the best way to do that?</p>
  
Tags: <plugin-development><error-templates><error-message>
Title: Render generic message/error template from plugin
ThreadText: # Render generic message/error template from plugin
  
  (Asked by: username_0 on 2014-06-25)
  
  Basically, I'm trying to emulate `show_error` from EE/CI: <http://ellislab.com/codeigniter/user-guide/general/errors.html>
  
  I want to just use the generic Craft error template (`/craft/app/templates/error.html`), like what happens if you have a DB connection error, and have it serve a 500.
  
  This is from with a plugin controller.
  
  What's the best way to do that?
  
  **Tags:** plugin-development, error-templates, error-message
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2014-06-25. Score: 9 | Accepted answer
  
  I'm not familiar with EE's show\_error, but a plugin's error template logic is identical to the logic that Craft uses to determine what to display.
  
  From your plugin, if you:
  
  ```
  throw new HttpException($statusCode, $optionalMessage)
  
  ```
  
  Where $statusCode is 404 or 400, or any proper [http status code](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes), Craft will load the corresponding 404.html, 400.html, etc. from the craft/app/templates folder displaying the optional message, or the more generic message supplied in the template if no optional one is provided.
  
  However, if you have supplied your own 404, 400, etc. from your own craft/templates folder, Craft will display that one instead of the one it provides so you can customize these errors for your site.
  
  If you:
  
  ```
  throw new Exception($optionalMessage)
  
  ```
  
  while [devMode](http://buildwithcraft.com/docs/config-settings#devMode) is enabled, then Craft will load exception.html which will display the full error long with stack trace to help in debugging. If devMode is not enabled, it will display the more generic error.html.
  
  Craft specifically checks for database connection errors and will display the error.html template while setting a 500 status code.
  
  
  ---
  
  ### Answer by username_1 on 2014-06-25. Score: 2
  
  This will do it, and you can change the 400 to whatever type of [http error](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes) you want:
  
  ```twig
  throw new HttpException(400, 'Error message text');
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2014-06-25. Score: 2
  
  Craft itself (I'm referencing the EntriesController) checks to see if an entry is saved. If it doesn't it, points back to the template and hands it an error message in the session and any needed variables.
  
  In the case of your plugin, gather your form data and errors and send it back. Craft does it all in a single Model. You can do the same or different. It doesn't matter so long as those using the plugin knows what's up.
  
  Example based on a Craft Controller (with optional Ajax response):
  
  ```
  if (craft()->request->isAjaxRequest())
  {
      $this->returnJson(array(
          'errors' => $errors,
      ));
  }
  else
  {
      $userSessionService->setError(Craft::t('Error Message!'));
  
      // Send the data back to the template.
      craft()->urlManager->setRouteVariables(array(
          'data' => $data,
          'errors' => $errors,
      ));
  }
  
  ```
  
  
  ---
  
  