Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
bea7cf84
Commit
bea7cf84
authored
May 07, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved exception handling to schema
parent
5cb24302
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
29 deletions
+27
-29
Command.php
framework/db/Command.php
+2
-29
Schema.php
framework/db/Schema.php
+25
-0
No files found.
framework/db/Command.php
View file @
bea7cf84
...
...
@@ -284,7 +284,7 @@ class Command extends \yii\base\Component
return
$n
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
$this
->
handleException
(
$e
,
$rawSql
);
$this
->
db
->
getSchema
()
->
handleException
(
$e
,
$rawSql
);
}
}
...
...
@@ -417,7 +417,7 @@ class Command extends \yii\base\Component
return
$result
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
'yii\db\Command::query'
);
$this
->
handleException
(
$e
,
$rawSql
);
$this
->
db
->
getSchema
()
->
handleException
(
$e
,
$rawSql
);
}
}
...
...
@@ -767,31 +767,4 @@ class Command extends \yii\base\Component
return
$this
->
setSql
(
$sql
);
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
protected
function
handleException
(
\Exception
$e
,
$rawSql
)
{
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$exceptionClass
=
'\yii\db\Exception'
;
$schema
=
$this
->
db
->
getSchema
();
$exceptionMap
=
$schema
->
exceptionMap
;
foreach
(
$exceptionMap
as
$error
=>
$class
)
{
if
(
strpos
(
$e
->
getMessage
(),
$error
)
!==
false
)
{
$exceptionClass
=
$class
;
}
}
$message
=
$e
->
getMessage
()
.
"
\n
The SQL being executed was:
$rawSql
"
;
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
$exceptionClass
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
(),
$e
);
}
}
}
framework/db/Schema.php
View file @
bea7cf84
...
...
@@ -478,4 +478,29 @@ abstract class Schema extends Object
return
'string'
;
}
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
protected
function
handleException
(
\Exception
$e
,
$rawSql
)
{
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$exceptionClass
=
'\yii\db\Exception'
;
foreach
(
$this
->
exceptionMap
as
$error
=>
$class
)
{
if
(
strpos
(
$e
->
getMessage
(),
$error
)
!==
false
)
{
$exceptionClass
=
$class
;
}
}
$message
=
$e
->
getMessage
()
.
"
\n
The SQL being executed was:
$rawSql
"
;
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
$exceptionClass
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
(),
$e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment