Return error instead of panic

Return the error instead of panic