Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit a0aa66b3
authored
Feb 12, 2015
by
Dr. Evil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New ExclusionConstraintError and handling it in Postgres
1 parent
32474460
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
5 deletions
lib/dialects/postgres/query.js
lib/errors.js
lib/dialects/postgres/query.js
View file @
a0aa66b
...
...
@@ -121,7 +121,7 @@ module.exports = (function() {
attribute
:
field
,
collate
:
attribute
.
match
(
/COLLATE "
(
.*
?)
"/
)
?
/COLLATE "
(
.*
?)
"/
.
exec
(
attribute
)[
1
]
:
undefined
,
order
:
attribute
.
indexOf
(
'DESC'
)
!==
-
1
?
'DESC'
:
attribute
.
indexOf
(
'ASC'
)
!==
-
1
?
'ASC'
:
undefined
,
length
:
undefined
,
length
:
undefined
};
});
delete
result
.
columns
;
...
...
@@ -254,7 +254,10 @@ module.exports = (function() {
Query
.
prototype
.
formatError
=
function
(
err
)
{
var
match
,
table
,
index
;
,
index
,
fields
,
errors
,
message
;
var
code
=
err
.
code
||
err
.
sqlState
,
errMessage
=
err
.
message
||
err
.
messagePrimary
...
...
@@ -277,9 +280,9 @@ module.exports = (function() {
match
=
errDetail
.
match
(
/Key
\((
.*
?)\)
=
\((
.*
?)\)
/
);
if
(
match
)
{
var
fields
=
Utils
.
_
.
zipObject
(
match
[
1
].
split
(
', '
),
match
[
2
].
split
(
', '
))
,
errors
=
[]
,
message
=
'Validation error'
;
fields
=
Utils
.
_
.
zipObject
(
match
[
1
].
split
(
', '
),
match
[
2
].
split
(
', '
));
errors
=
[];
message
=
'Validation error'
;
Utils
.
_
.
forOwn
(
fields
,
function
(
value
,
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
...
...
@@ -309,6 +312,44 @@ module.exports = (function() {
}
break
;
case
'23P01'
:
// there are multiple different formats of error messages for this error code
// this regex should check at least two
match
=
errDetail
.
match
(
/Key
\((
.*
?)\)
=
\((
.*
?)\)
/
);
if
(
match
)
{
fields
=
Utils
.
_
.
zipObject
(
match
[
1
].
split
(
', '
),
match
[
2
].
split
(
', '
));
errors
=
[];
message
=
'Validation error'
;
Utils
.
_
.
forOwn
(
fields
,
function
(
value
,
field
)
{
errors
.
push
(
new
sequelizeErrors
.
ValidationErrorItem
(
field
+
' must be unique'
,
'unique violation'
,
field
,
value
));
});
if
(
this
.
callee
&&
this
.
callee
.
__options
&&
this
.
callee
.
__options
.
uniqueKeys
)
{
Utils
.
_
.
forOwn
(
this
.
callee
.
__options
.
uniqueKeys
,
function
(
constraint
)
{
if
(
Utils
.
_
.
isEqual
(
constraint
.
fields
,
Object
.
keys
(
fields
))
&&
!!
constraint
.
msg
)
{
message
=
constraint
.
msg
;
return
false
;
}
});
}
return
new
sequelizeErrors
.
ExclusionConstraintError
({
message
:
message
,
errors
:
errors
,
parent
:
err
,
fields
:
fields
});
}
else
{
return
new
sequelizeErrors
.
ExclusionConstraintError
({
message
:
errMessage
,
parent
:
err
});
}
break
;
default
:
return
new
sequelizeErrors
.
DatabaseError
(
err
);
}
...
...
lib/errors.js
View file @
a0aa66b
...
...
@@ -145,6 +145,26 @@ error.ForeignKeyConstraintError = function (options) {
util
.
inherits
(
error
.
ForeignKeyConstraintError
,
error
.
DatabaseError
);
/**
* Thrown when an exclusion constraint is violated in the database
* @extends DatabaseError
* @constructor
*/
error
.
ExclusionConstraintError
=
function
(
options
)
{
options
=
options
||
{};
options
.
parent
=
options
.
parent
||
{
sql
:
''
};
error
.
DatabaseError
.
call
(
this
,
options
.
parent
);
this
.
name
=
'SequelizeExclusionConstraintError'
;
this
.
message
=
options
.
message
;
this
.
fields
=
options
.
fields
;
this
.
table
=
options
.
table
;
this
.
value
=
options
.
value
;
this
.
index
=
options
.
index
;
};
util
.
inherits
(
error
.
ExclusionConstraintError
,
error
.
DatabaseError
);
/**
* The message from the DB.
* @property message
* @name message
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment