input: Write a function that generates a REST API using Flask and SQLAlchemy to manage records in a database, allowing users to perform the CRUD operations (Create, Read, Update, Delete). The generated API should include endpoints to retrieve all records, create a new record, update an existing record, and delete a record. If the provided database URI is invalid, the function should handle the exception and return an error message.
  assert generate_rest_api() == "REST API successfully created"
  assert generate_rest_api(database_uri="invalid_uri") == "Error: Invalid database URI"
output: def generate_rest_api(database_uri="sqlite:///example.db"):
      from flask import Flask, request, jsonify
      from flask_sqlalchemy import SQLAlchemy
      import os
  
      app = Flask(__name__)
      app.config['SQLALCHEMY_DATABASE_URI'] = database_uri
      app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  
      try:
          db = SQLAlchemy(app)
      except Exception as e:
          return "Error: Invalid database URI"
  
      class Record(db.Model):
          id = db.Column(db.Integer, primary_key=True)
          name = db.Column(db.String(80), nullable=False)
          value = db.Column(db.String(120), nullable=False)
  
          def to_dict(self):
              return {'id': self.id, 'name': self.name, 'value': self.value}
  
      @app.route('/records', methods=['GET'])
      def get_records():
          records = Record.query.all()
          return jsonify([record.to_dict() for record in records])
  
      @app.route('/records', methods=['POST'])
      def create_record():
          data = request.get_json()
          new_record = Record(name=data['name'], value=data['value'])
          db.session.add(new_record)
          db.session.commit()
          return jsonify(new_record.to_dict()), 201
  
      @app.route('/records/<int:id>', methods=['PUT'])
      def update_record(id):
          data = request.get_json()
          record = Record.query.get(id)
          if record:
              record.name = data['name']
              record.value = data['value']
              db.session.commit()
              return jsonify(record.to_dict())
          return jsonify({'error': 'Record not found'}), 404
  
      @app.route('/records/<int:id>', methods=['DELETE'])
      def delete_record(id):
          record = Record.query.get(id)
          if record:
              db.session.delete(record)
              db.session.commit()
              return jsonify({'result': 'Record deleted'})
          return jsonify({'error': 'Record not found'}), 404
  
      db.create_all()
      return "REST API successfully created"