refactor error handling for more expressive error types