I suspect that this pattern could also be factored out into a function.  e.g error_response(ErrCode, Msg, Ctx) -> ...