diff --git a/Elwig/Services/InteractionService.cs b/Elwig/Services/InteractionService.cs index 83acbf2..1e7efef 100644 --- a/Elwig/Services/InteractionService.cs +++ b/Elwig/Services/InteractionService.cs @@ -1,3 +1,4 @@ +using Microsoft.Data.Sqlite; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -117,8 +118,35 @@ namespace Elwig.Services { public static void ShowException(string title, string? text, Exception exc, bool showExcType = false, bool isError = true) { text = text == null ? "" : text + (text.EndsWith('.') || text.EndsWith('!') || text.EndsWith('?') ? "" : ":") + "\n\n"; - text += exc.Message + (showExcType ? $" ({exc.GetType().Name})" : ""); - if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; + if ((exc.InnerException ?? exc) is SqliteException sql) { + if (sql.SqliteErrorCode == 5 || sql.SqliteErrorCode == 6) { + // SQLITE_BUSY / SQLITE_LOCKED + text += "Die Datenbank wurde gerade von einem anderen Gerät/Prozess verwendet. Bitte noch einmal versuchen!\n\n" + sql.Message; + } else if (sql.SqliteErrorCode == 19) { + // SQLITE_CONSTRAINT + switch (sql.SqliteExtendedErrorCode) { + case 1555: // SQLITE_CONSTRAINT_PRIMARYKEY - indicating that a PRIMARY KEY constraint failed + case 2067: // SQLITE_CONSTRAINT_UNIQUE - indicating that a UNIQUE constraint failed + case 2579: // SQLITE_CONSTRAINT_ROWID - indicating that a rowid is not unique + text += "Dieser Datensatz existiert bereits!\n\n"; + break; + case 787: // SQLITE_CONSTRAINT_FOREIGNKEY - indicating that a foreign key constraint failed + case 1299: // SQLITE_CONSTRAINT_NOTNULL - indicating that a NOT NULL constraint failed + case 275: // SQLITE_CONSTRAINT_CHECK - indicating that a CHECK constraint failed + case 1811: // SQLITE_CONSTRAINT_TRIGGER - indicating that a RAISE function within a trigger fired, causing the SQL statement to abort + case 531: // SQLITE_CONSTRAINT_COMMITHOOK - indicating that a commit hook callback returned non-zero that thus caused the SQL statement to be rolled back + text += "Der Datensatz ist inkonsistent!\n\n"; + break; + } + text += sql.Message; + } else { + text += exc.Message + (showExcType ? $" ({exc.GetType().Name})" : ""); + if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; + } + } else { + text += exc.Message + (showExcType ? $" ({exc.GetType().Name})" : ""); + if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; + } if (isError) { ShowError(title, text); } else {