|
@@ -65,40 +65,39 @@ returnResponse(500, $exception->getTraceAsString(), true, 'Internal Server Error
|
|
|
returnResponse(404, null, true, 'The requested resource was not found.');
|
|
|
*/
|
|
|
|
|
|
+// Function server error
|
|
|
+function serverError($title, $message)
|
|
|
+{
|
|
|
+ header('Content-Type: text/html');
|
|
|
+ http_response_code(500);
|
|
|
+
|
|
|
+ require "{$_SERVER['DOCUMENT_ROOT']}/action/error.php";
|
|
|
+ exit();
|
|
|
+}
|
|
|
+
|
|
|
// DOTENV: load environment variables from .env file
|
|
|
try {
|
|
|
$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']);
|
|
|
$dotenv->load();
|
|
|
} catch (Dotenv\Exception\InvalidPathException $e) {
|
|
|
- header('Content-Type: application/json');
|
|
|
- http_response_code(500);
|
|
|
-
|
|
|
- echo json_encode(array(
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'mensaje' => 'No se pudo cargar el archivo .env en la raiz del proyecto'
|
|
|
- ));
|
|
|
-
|
|
|
+ serverError('Error de configuración', 'No se pudo cargar el archivo de configuración');
|
|
|
exit();
|
|
|
}
|
|
|
|
|
|
// POSTGRES: load PostgresDb class
|
|
|
use \SeinopSys\PostgresDb;
|
|
|
|
|
|
-function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $user = null, $password = null): PostgresDb {
|
|
|
+function makeConnection($hostOrConnectionString, $port = null, $dbname = null, $user = null, $password = null): PostgresDb
|
|
|
+{
|
|
|
$connectionString = is_null($port) ? $hostOrConnectionString : "pgsql:host=$hostOrConnectionString;port=$port;dbname=$dbname;user=$user;password=$password";
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
$pdo = new PDO($connectionString);
|
|
|
$db = new PostgresDb();
|
|
|
$db->setConnection($pdo);
|
|
|
return $db;
|
|
|
} catch (PDOException $e) {
|
|
|
- header('Content-Type: application/json');
|
|
|
- http_response_code(500);
|
|
|
- echo json_encode([
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'mensaje' => "No se pudo conectar a la base de datos" . (is_null($dbname) ? "" : " $dbname")
|
|
|
- ]);
|
|
|
+ serverError('Error de conexión', 'No se pudo conectar a la base de datos');
|
|
|
exit();
|
|
|
}
|
|
|
}
|