Fix error handling when passed nil or @""

- Add an error domain and error codes.
- Handle empty string without an assertion.
- Return an error when returning nil (per conventions).