Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving exception msgs for database types #11764

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Database/Type/BoolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public function toDatabase($value, Driver $driver)
return (bool)$value;
}

throw new InvalidArgumentException('Cannot convert value to bool');
throw new InvalidArgumentException(sprintf(
'Cannot convert value of type `%s` to bool',
getTypeName($value)
Copy link
Member

@dereuromark dereuromark Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldnt it then also make sense to include the value itself directly? Or is that a bit too much then as debug info?

'Cannot convert value `%s` of type `%s` to bool'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do that, but how could we archive this? It's just the base \InvalidArgumentException and doesn't really have a way to attach arbitrary data to it? Also does the error renderer has the capability of showing that value? Especially in this case of wrong type values, know the content of $value would make debugging the cause extremely helpful.

Copy link
Member

@dereuromark dereuromark Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe a bit out of scope - or even overkill - to use some Debugger::exportVar($x, 0).
Hopefully the stracktrace contains the caller arguments.

));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Type/DecimalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public function toDatabase($value, Driver $driver)
return null;
}
if (!is_scalar($value)) {
throw new InvalidArgumentException('Cannot convert value to a decimal.');
throw new InvalidArgumentException(sprintf(
'Cannot convert value of type `%s` to a decimal',
getTypeName($value)
));
}
if (is_string($value) && is_numeric($value)) {
return $value;
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public function toDatabase($value, Driver $driver)
}

if (!is_scalar($value)) {
throw new InvalidArgumentException('Cannot convert value to integer');
throw new InvalidArgumentException(sprintf(
'Cannot convert value of type `%s` to integer',
getTypeName($value)
));
}

return (int)$value;
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public function toDatabase($value, Driver $driver)
return (string)$value;
}

throw new InvalidArgumentException('Cannot convert value to string');
throw new InvalidArgumentException(sprintf(
'Cannot convert value of type `%s` to string',
getTypeName($value)
));
}

/**
Expand Down