| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-14 09:59:12 +00:00
										 |  |  | //go:build moderncsqlite3 || nowasm | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | package sqlite | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"database/sql/driver" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 09:35:57 +02:00
										 |  |  | 	"modernc.org/sqlite" | 
					
						
							|  |  |  | 	sqlite3 "modernc.org/sqlite/lib" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/db" | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // processSQLiteError processes an sqlite3.Error to | 
					
						
							|  |  |  | // handle conversion to any of our common db types. | 
					
						
							|  |  |  | func processSQLiteError(err error) error { | 
					
						
							|  |  |  | 	// Attempt to cast as sqlite error. | 
					
						
							| 
									
										
										
										
											2024-06-29 09:35:57 +02:00
										 |  |  | 	sqliteErr, ok := err.(*sqlite.Error) | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Handle supplied error code: | 
					
						
							| 
									
										
										
										
											2024-06-29 09:35:57 +02:00
										 |  |  | 	switch sqliteErr.Code() { | 
					
						
							|  |  |  | 	case sqlite3.SQLITE_CONSTRAINT_UNIQUE, | 
					
						
							|  |  |  | 		sqlite3.SQLITE_CONSTRAINT_PRIMARYKEY: | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | 		return db.ErrAlreadyExists | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-29 09:35:57 +02:00
										 |  |  | 	// Busy should be very rare, but | 
					
						
							|  |  |  | 	// on busy tell the database to close | 
					
						
							|  |  |  | 	// the connection, re-open and re-attempt | 
					
						
							|  |  |  | 	// which should give a necessary timeout. | 
					
						
							|  |  |  | 	case sqlite3.SQLITE_BUSY, | 
					
						
							|  |  |  | 		sqlite3.SQLITE_BUSY_RECOVERY, | 
					
						
							|  |  |  | 		sqlite3.SQLITE_BUSY_SNAPSHOT: | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | 		return driver.ErrBadConn | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wrap the returned error with the code and | 
					
						
							|  |  |  | 	// extended code for easier debugging later. | 
					
						
							| 
									
										
										
										
											2024-06-29 09:35:57 +02:00
										 |  |  | 	return fmt.Errorf("%w (code=%d)", err, | 
					
						
							| 
									
										
										
										
											2024-05-27 15:46:15 +00:00
										 |  |  | 		sqliteErr.Code(), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } |