From a884a0cb939978393d01c6d5f28da9f94480843f Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Fri, 28 Jan 2011 08:39:03 +0000
Subject: [PATCH] SimpleSAML_Error: Support for custom error codes.

Thanks to Andjelko Horvat for implementing this.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2724 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Error/Error.php | 67 ++++++++++++++++++++++++++++++++++
 templates/error.php            |  4 +-
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php
index 796681c42..af86ae4c0 100644
--- a/lib/SimpleSAML/Error/Error.php
+++ b/lib/SimpleSAML/Error/Error.php
@@ -18,6 +18,30 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	private $errorCode;
 
 
+	/**
+	 * The error title tag in dictionary.
+	 *
+	 * @var string
+	 */
+	private $dictTitle;
+
+
+	/**
+	 * The error description tag in dictionary.
+	 *
+	 * @var string
+	 */
+	private $dictDescr;
+
+
+	/**
+	 * The name of module which throw error.
+	 *
+	 * @var string|NULL
+	 */
+	private $module = NULL;
+
+
 	/**
 	 * The parameters for the error.
 	 *
@@ -48,6 +72,16 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 			$this->errorCode = $errorCode;
 		}
 
+		$moduleCode = explode(':', $this->errorCode, 2);
+		if (count($moduleCode) === 2) {
+			$this->module = $moduleCode[0];
+			$this->dictTitle = '{' . $this->module . ':errors:title_' . $moduleCode[1] . '}';
+			$this->dictDescr = '{' . $this->module . ':errors:descr_' . $moduleCode[1] . '}';
+		} else {
+			$this->dictTitle = '{errors:title_' . $this->errorCode . '}';
+			$this->dictDescr = '{errors:descr_' . $this->errorCode . '}';
+		}
+
 		if (!empty($this->parameters)) {
 			$msg = $this->errorCode . '(';
 			foreach ($this->parameters as $k => $v) {
@@ -75,6 +109,36 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	}
 
 
+	/**
+	 * Retrieve the error parameters given when throwing this error.
+	 *
+	 * @return array  The parameters.
+	 */
+	public function getParameters() {
+		return $this->parameters;
+	}
+
+
+	/**
+	 * Retrieve the error title tag in dictionary.
+	 *
+	 * @return string  The error title tag.
+	 */
+	public function getDictTitle() {
+		return $this->dictTitle;
+	}
+
+
+	/**
+	 * Retrieve the error description tag in dictionary.
+	 *
+	 * @return string  The error description tag.
+	 */
+	public function getDictDescr() {
+		return $this->dictDescr;
+	}
+
+
 	/**
 	 * Set the HTTP return code for this error.
 	 *
@@ -150,6 +214,9 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 		$data['error'] = $errorData;
 		$data['errorCode'] = $this->errorCode;
 		$data['parameters'] = $this->parameters;
+		$data['module'] = $this->module;
+		$data['dictTitle'] = $this->dictTitle;
+		$data['dictDescr'] = $this->dictDescr;
 
 		/* Check if there is a valid technical contact email address. */
 		if($config->getString('technicalcontact_email', 'na@example.org') !== 'na@example.org') {
diff --git a/templates/error.php b/templates/error.php
index c40fa0790..b9603a435 100644
--- a/templates/error.php
+++ b/templates/error.php
@@ -9,10 +9,10 @@
 ?>
 
 
-	<h2><?php echo $this->t('title_' . $this->data['errorCode']); ?></h2>
+	<h2><?php echo $this->t($this->data['dictTitle']); ?></h2>
 
 <?php
-echo htmlspecialchars($this->t('descr_' . $this->data['errorCode'], $this->data['parameters']));
+echo htmlspecialchars($this->t($this->data['dictDescr'], $this->data['parameters']));
 ?>
 
 	<div class="trackidtext">
-- 
GitLab