Selaa lähdekoodia

Refactor error handling and add Moodle database connection

Alejandro Rosales 1 vuosi sitten
vanhempi
commit
7e39a3868c
1 muutettua tiedostoa jossa 4 lisäystä ja 3 poistoa
  1. 4 3
      dependencies.php

+ 4 - 3
dependencies.php

@@ -18,8 +18,7 @@ function methods($methodSchemas)
     // Validate against the schema for the current request method
     $schema = $methodSchemas[$requestMethod];
     if (!$schema->validate($data)) {
-        http_response_code(403);
-        echo json_encode(['error' => 'Invalid input']);
+        serverError('Error de validación', 'Los datos enviados no son válidos');
         exit();
     }
 }
@@ -97,7 +96,7 @@ function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $
         $db->setConnection($pdo);
         return $db;
     } catch (PDOException $e) {
-        serverError('Error de conexión', 'No se pudo conectar a la base de datos');
+        serverError('Error de conexión', 'No se pudo conectar a la base de datos: ' . $e->getMessage() . ' ' . $connectionString);
         exit();
     }
 }
@@ -105,3 +104,5 @@ function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $
 # default DB connection
 $db = makeConnection($_ENV['POSTGRES_HOST'], $_ENV['POSTGRES_PORT'], $_ENV['POSTGRES_DBNAME'], $_ENV['POSTGRES_USER'], $_ENV['POSTGRES_PASSWORD']);
 $sgi_db = makeConnection($_ENV['SGI_POSTGRES_HOST'], $_ENV['SGI_POSTGRES_PORT'], $_ENV['SGI_POSTGRES_DBNAME'], $_ENV['SGI_POSTGRES_USER'], $_ENV['SGI_POSTGRES_PASSWORD']);
+if (isset($_SESSION['moodle_db']))
+    $moodle_db = makeConnection($_SESSION['moodle_db']);