Refactor url error handling