Skip to content

Commit

Permalink
refine a few InvalidArgException translation cases
Browse files Browse the repository at this point in the history
This change fixes the error message displayed to the user after an
InvalidArgException exception (hard) is converted into an
InvalidRequestArgException one (soft).

InvalidArgException::__construct(): keep copies of arguments
InvalidArgException::getName(): new read accessor
InvalidArgException::getValue(): idem
InvalidArgException::getReason(): idem
convertToIRAE(): new function to convert between two classes
assertIPArg(): update to use above
assertIPv4Arg(): idem
assertIPv6Arg(): idem
  • Loading branch information
infrastation committed Mar 2, 2013
1 parent bc6499d commit 3be783f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 15 additions & 0 deletions wwwroot/inc/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ function __construct ($name, $value, $reason=NULL)
$message = "Argument '${name}' of value " . var_export ($value, TRUE) . ' is invalid';
$message .= is_null ($reason) ? '.' : " (${reason}).";
parent::__construct ($message, parent::INTERNAL);
$this->name = $name;
$this->value = $value;
$this->reason = $reason;
}
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function getReason()
{
return $this->reason;
}
}

Expand Down
26 changes: 23 additions & 3 deletions wwwroot/inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function assertIPArg ($argname)
}
catch (InvalidArgException $e)
{
throw new InvalidRequestArgException ($argname, $_REQUEST[$argname], $e->getMessage());
throw convertToIRAE ($e, $argname);
}
}

Expand All @@ -227,7 +227,7 @@ function assertIPv4Arg ($argname)
}
catch (InvalidArgException $e)
{
throw new InvalidRequestArgException ($argname, $_REQUEST[$argname], $e->getMessage());
throw convertToIRAE ($e, $argname);
}
}

Expand All @@ -240,7 +240,7 @@ function assertIPv6Arg ($argname)
}
catch (InvalidArgException $e)
{
throw new InvalidRequestArgException ($argname, $_REQUEST[$argname], $e->getMessage());
throw convertToIRAE ($e, $argname);
}
}

Expand Down Expand Up @@ -5869,4 +5869,24 @@ function checkPortRole ($vswitch, $port_name, $port_order)
return TRUE; // not linked port
}

# Convert InvalidArgException to InvalidRequestArgException with a choice of
# replacing the reference to the failed argument or leaving it unchanged.
function convertToIRAE ($iae, $override_argname = NULL)
{
if (! ($iae instanceof InvalidArgException))
throw new InvalidArgException ('iae', '(object)', 'not an instance of InvalidArgException class');

if (is_null ($override_argname))
{
$reported_argname = $iae->getName();
$reported_argvalue = $iae->getValue();
}
else
{
$reported_argname = $override_argname;
$reported_argvalue = $_REQUEST[$override_argname];
}
return new InvalidRequestArgException ($reported_argname, $reported_argvalue, $iae->getReason());
}

?>

0 comments on commit 3be783f

Please sign in to comment.