| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | package bundb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/jackc/pgconn" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"modernc.org/sqlite" | 
					
						
							|  |  |  | 	sqlite3 "modernc.org/sqlite/lib" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // processPostgresError processes an error, replacing any postgres specific errors with our own error type | 
					
						
							|  |  |  | func processPostgresError(err error) db.Error { | 
					
						
							|  |  |  | 	// Attempt to cast as postgres | 
					
						
							|  |  |  | 	pgErr, ok := err.(*pgconn.PgError) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Handle supplied error code: | 
					
						
							|  |  |  | 	// (https://www.postgresql.org/docs/10/errcodes-appendix.html) | 
					
						
							|  |  |  | 	switch pgErr.Code { | 
					
						
							|  |  |  | 	case "23505" /* unique_violation */ : | 
					
						
							| 
									
										
										
										
											2022-09-12 13:03:23 +02:00
										 |  |  | 		return db.ErrAlreadyExists | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // processSQLiteError processes an error, replacing any sqlite specific errors with our own error type | 
					
						
							|  |  |  | func processSQLiteError(err error) db.Error { | 
					
						
							|  |  |  | 	// Attempt to cast as sqlite | 
					
						
							|  |  |  | 	sqliteErr, ok := err.(*sqlite.Error) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Handle supplied error code: | 
					
						
							|  |  |  | 	switch sqliteErr.Code() { | 
					
						
							| 
									
										
										
										
											2022-01-08 17:17:01 +01:00
										 |  |  | 	case sqlite3.SQLITE_CONSTRAINT_UNIQUE, sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY: | 
					
						
							| 
									
										
										
										
											2022-09-12 13:03:23 +02:00
										 |  |  | 		return db.ErrAlreadyExists | 
					
						
							| 
									
										
										
										
											2021-08-29 15:41:41 +01:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |