connectionName = $connectionName; $this->sql = $sql; $this->bindings = $bindings; $this->connectionDetails = $connectionDetails; $this->readWriteType = $readWriteType; $this->code = $previous->getCode(); $this->message = $this->formatMessage($connectionName, $sql, $bindings, $previous); if ($previous instanceof PDOException) { $this->errorInfo = $previous->errorInfo; } } /** * Format the SQL error message. * * @param string $connectionName * @param string $sql * @param array $bindings * @param \Throwable $previous * @return string */ protected function formatMessage($connectionName, $sql, $bindings, Throwable $previous) { $details = $this->formatConnectionDetails(); return $previous->getMessage().' (Connection: '.$connectionName.$details.', SQL: '.Str::replaceArray('?', $bindings, $sql).')'; } /** * Format the connection details for the error message. * * @return string */ protected function formatConnectionDetails() { if (empty($this->connectionDetails)) { return ''; } $driver = $this->connectionDetails['driver'] ?? ''; $segments = []; if ($driver !== 'sqlite') { if (! empty($this->connectionDetails['unix_socket'])) { $segments[] = 'Socket: '.$this->connectionDetails['unix_socket']; } else { $host = $this->connectionDetails['host'] ?? ''; $segments[] = 'Host: '.(is_array($host) ? implode(', ', $host) : $host); $segments[] = 'Port: '.($this->connectionDetails['port'] ?? ''); } } $segments[] = 'Database: '.($this->connectionDetails['database'] ?? ''); return ', '.implode(', ', $segments); } /** * Get the connection name for the query. * * @return string */ public function getConnectionName() { return $this->connectionName; } /** * Get the SQL for the query. * * @return string */ public function getSql() { return $this->sql; } /** * Get the raw SQL representation of the query with embedded bindings. */ public function getRawSql(): string { return DB::connection($this->getConnectionName()) ->getQueryGrammar() ->substituteBindingsIntoRawSql($this->getSql(), $this->getBindings()); } /** * Get the bindings for the query. * * @return array */ public function getBindings() { return $this->bindings; } /** * Get information about the connection such as host, port, database, etc. * * @return array */ public function getConnectionDetails() { return $this->connectionDetails; } }