-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from EmergingDigitalAcademy/sequelize-validation
added model validation using sequelize
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ValidationError, DatabaseError, UniqueConstraintError, ForeignKeyConstraintError } from 'sequelize'; | ||
|
||
export const sequelizeErrorHandler = (error, req, res, next) => { | ||
if (error instanceof ValidationError) { | ||
return res.status(400).json({ | ||
error: error.errors.map(e => e.message) | ||
}); | ||
} | ||
|
||
if (error instanceof UniqueConstraintError) { | ||
return res.status(409).json({ | ||
error: 'Resource already exists' | ||
}); | ||
} | ||
|
||
if (error instanceof ForeignKeyConstraintError) { | ||
return res.status(400).json({ | ||
error: 'Invalid reference to related resource' | ||
}); | ||
} | ||
|
||
if (error instanceof DatabaseError) { | ||
console.error('Database error:', error); | ||
return res.status(500).json({ | ||
error: 'A database error occurred' | ||
}); | ||
} | ||
|
||
console.error('Unexpected error:', error); | ||
res.status(500).json({ | ||
error: 'An unexpected error occurred' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters