mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-30 17:37:26 +00:00 
			
		
		
		
	 808b2a464f
			
		
	
	
		808b2a464f
		
			
		
	
	
	
	
		
			
			* Handle duplicate entry error message in bookshelf connector Signed-off-by: jozefcipa <jozef.cipa@strv.com> * Handle duplicate error message in mongoose connector Signed-off-by: jozefcipa <jozef.cipa@strv.com> * Apply changes from review * Fix wrapping transactions in bookshelf adapter * Add missing await to wrapErrors * Call findOne without this
		
			
				
	
	
		
			33 lines
		
	
	
		
			557 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			557 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const isDuplicateEntryError = error => {
 | |
|   // postgres
 | |
|   if (error.code === '23505') {
 | |
|     return true;
 | |
|   }
 | |
| 
 | |
|   // mysql
 | |
|   if (error.code === 'ER_DUP_ENTRY') {
 | |
|     return true;
 | |
|   }
 | |
| 
 | |
|   // sqlite
 | |
|   if (error.toString().includes('SQLITE_CONSTRAINT: UNIQUE')) {
 | |
|     return true;
 | |
|   }
 | |
| 
 | |
|   return false;
 | |
| };
 | |
| 
 | |
| const handleDatabaseError = error => {
 | |
|   if (isDuplicateEntryError(error)) {
 | |
|     strapi.log.warn('Duplicate entry', error.toString());
 | |
|     throw new Error('Duplicate entry');
 | |
|   }
 | |
|   throw error;
 | |
| };
 | |
| 
 | |
| module.exports = {
 | |
|   handleDatabaseError,
 | |
| };
 |