Skip to content
Permalink
Browse files

Removing the usage of the cryptic SocketException in the Email class

The exception was being thrown when invalid arguments where passed, giving
the impression that a problem talking to the network happened
  • Loading branch information...
lorenzo committed Aug 30, 2014
1 parent d6dcee4 commit cbbb29ff00cd61636388f9dc61f2886fced8d53b
Showing with 33 additions and 38 deletions.
  1. +33 −38 src/Network/Email/Email.php
@@ -14,17 +14,20 @@
*/
namespace Cake\Network\Email;
use BadMethodCallException;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\StaticConfigTrait;
use Cake\Core\Exception\Exception;
use Cake\Core\StaticConfigTrait;
use Cake\Log\Log;
use Cake\Network\Error;
use Cake\Network\Http\FormData\Part;
use Cake\Utility\File;
use Cake\Utility\Hash;
use Cake\Utility\String;
use Cake\View\View;
use InvalidArgumentException;
use LogicException;
/**
* CakePHP email class.
@@ -374,7 +377,7 @@ public function __construct($config = null) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return array|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function from($email = null, $name = null) {
if ($email === null) {
@@ -390,7 +393,7 @@ public function from($email = null, $name = null) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return array|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function sender($email = null, $name = null) {
if ($email === null) {
@@ -406,7 +409,7 @@ public function sender($email = null, $name = null) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return array|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function replyTo($email = null, $name = null) {
if ($email === null) {
@@ -422,7 +425,7 @@ public function replyTo($email = null, $name = null) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return array|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function readReceipt($email = null, $name = null) {
if ($email === null) {
@@ -438,7 +441,7 @@ public function readReceipt($email = null, $name = null) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return array|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function returnPath($email = null, $name = null) {
if ($email === null) {
@@ -580,7 +583,7 @@ public function emailPattern($regex = false) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return $this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
protected function _setEmail($varName, $email, $name) {
if (!is_array($email)) {
@@ -608,15 +611,15 @@ protected function _setEmail($varName, $email, $name) {
*
* @param string $email Email address to validate
* @return void
* @throws \Cake\Network\Error\SocketException If email address does not validate
* @throws \InvalidArgumentException If email address does not validate
*/
protected function _validateEmail($email) {
if ($this->_emailPattern === null && filter_var($email, FILTER_VALIDATE_EMAIL)) {
return;
} elseif (preg_match($this->_emailPattern, $email)) {
return;
}
throw new Error\SocketException(sprintf('Invalid email: "%s"', $email));
throw new InvalidArgumentException(sprintf('Invalid email: "%s"', $email));
}
/**
@@ -628,14 +631,14 @@ protected function _validateEmail($email) {
* @param string $name Name
* @param string $throwMessage Exception message
* @return $this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
protected function _setEmailSingle($varName, $email, $name, $throwMessage) {
$current = $this->{$varName};
$this->_setEmail($varName, $email, $name);
if (count($this->{$varName}) !== 1) {
$this->{$varName} = $current;
throw new Error\SocketException($throwMessage);
throw new InvalidArgumentException($throwMessage);
}
return $this;
}
@@ -648,7 +651,7 @@ protected function _setEmailSingle($varName, $email, $name, $throwMessage) {
* Array with email as key, name as value or email as value (without name)
* @param string $name Name
* @return $this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
protected function _addEmail($varName, $email, $name) {
if (!is_array($email)) {
@@ -690,12 +693,8 @@ public function subject($subject = null) {
*
* @param array $headers Associative array containing headers to be set.
* @return $this
* @throws \Cake\Network\Error\SocketException
*/
public function setHeaders($headers) {
if (!is_array($headers)) {
throw new Error\SocketException('$headers should be an array.');
}
public function setHeaders(array $headers) {
$this->_headers = $headers;
return $this;
}
@@ -705,12 +704,8 @@ public function setHeaders($headers) {
*
* @param array $headers Headers to set.
* @return $this
* @throws \Cake\Network\Error\SocketException
*/
public function addHeaders($headers) {
if (!is_array($headers)) {
throw new Error\SocketException('$headers should be an array.');
}
public function addHeaders(array $headers) {
$this->_headers = array_merge($this->_headers, $headers);
return $this;
}
@@ -914,14 +909,14 @@ public function helpers($helpers = null) {
*
* @param string $format Formatting string.
* @return string|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function emailFormat($format = null) {
if ($format === null) {
return $this->_emailFormat;
}
if (!in_array($format, $this->_emailFormatAvailable)) {
throw new Error\SocketException('Format not available.');
throw new InvalidArgumentException('Format not available.');
}
$this->_emailFormat = $format;
return $this;
@@ -936,7 +931,7 @@ public function emailFormat($format = null) {
* @param string|AbstractTransport $name Either the name of a configured
* transport, or a transport instance.
* @return \Cake\Network\Email\AbstractTransport|$this
* @throws \Cake\Network\Error\SocketException When the chosen transport lacks a send method.
* @throws \LogicException When the chosen transport lacks a send method.
*/
public function transport($name = null) {
if ($name === null) {
@@ -949,7 +944,7 @@ public function transport($name = null) {
$transport = $name;
}
if (!method_exists($transport, 'send')) {
throw new Error\SocketException(sprintf('The "%s" do not have send method.', get_class($transport)));
throw new LogicException(sprintf('The "%s" do not have send method.', get_class($transport)));
}
$this->_transport = $transport;
@@ -990,7 +985,7 @@ protected function _constructTransport($name) {
*
* @param bool|string $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID
* @return bool|string|$this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function messageId($message = null) {
if ($message === null) {
@@ -1000,7 +995,7 @@ public function messageId($message = null) {
$this->_messageId = $message;
} else {
if (!preg_match('/^\<.+@.+\>$/', $message)) {
throw new Error\SocketException('Invalid format to Message-ID. The text should be something like "<uuid@server.com>"');
throw new InvalidArgumentException('Invalid format to Message-ID. The text should be something like "<uuid@server.com>"');
}
$this->_messageId = $message;
}
@@ -1066,7 +1061,7 @@ public function domain($domain = null) {
*
* @param string|array $attachments String with the filename or array with filenames
* @return array|$this Either the array of attachments when getting or $this when setting.
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public function attachments($attachments = null) {
if ($attachments === null) {
@@ -1079,17 +1074,17 @@ public function attachments($attachments = null) {
}
if (!isset($fileInfo['file'])) {
if (!isset($fileInfo['data'])) {
throw new Error\SocketException('No file or data specified.');
throw new InvalidArgumentException('No file or data specified.');
}
if (is_int($name)) {
throw new Error\SocketException('No filename specified.');
throw new InvalidArgumentException('No filename specified.');
}
$fileInfo['data'] = chunk_split(base64_encode($fileInfo['data']), 76, "\r\n");
} else {
$fileName = $fileInfo['file'];
$fileInfo['file'] = realpath($fileInfo['file']);
if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
throw new Error\SocketException(sprintf('File not found: "%s"', $fileName));
throw new InvalidArgumentException(sprintf('File not found: "%s"', $fileName));
}
if (is_int($name)) {
$name = basename($fileInfo['file']);
@@ -1109,7 +1104,7 @@ public function attachments($attachments = null) {
*
* @param string|array $attachments String with the filename or array with filenames
* @return $this
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
* @see \Cake\Network\Email\Email::attachments()
*/
public function addAttachments($attachments) {
@@ -1208,14 +1203,14 @@ public function profile($config = null) {
*
* @param string|array $content String with message or array with messages
* @return array
* @throws \Cake\Network\Error\SocketException
* @throws \BadMethodCallException
*/
public function send($content = null) {
if (empty($this->_from)) {
throw new Error\SocketException('From is not specified.');
throw new BadMethodCallException('From is not specified.');
}
if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
throw new Error\SocketException('You need specify one destination on to, cc or bcc.');
throw new BadMethodCallException('You need specify one destination on to, cc or bcc.');
}
if (is_array($content)) {
@@ -1228,7 +1223,7 @@ public function send($content = null) {
if (!$transport) {
$msg = 'Cannot send email, transport was not defined. Did you call transport() or define ' .
' a transport in the set profile?';
throw new Error\SocketException($msg);
throw new BadMethodCallException($msg);
}
$contents = $transport->send($this);
$this->_logDelivery($contents);
@@ -1271,7 +1266,7 @@ protected function _logDelivery($contents) {
* @param string|array $transportConfig String to use config from EmailConfig or array with configs
* @param bool $send Send the email or just return the instance pre-configured
* @return \Cake\Network\Email\Email Instance of Cake\Network\Email\Email
* @throws \Cake\Network\Error\SocketException
* @throws \InvalidArgumentException
*/
public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) {
$class = __CLASS__;

0 comments on commit cbbb29f

Please sign in to comment.
You can’t perform that action at this time.