fix response cast, handle non-HTTP responses, and define error code