/// <summary> /// Try to execute passed function catching common postgres exceptions. /// All caught exceptions will be processed and re-thrown. /// </summary> /// <param name="action">Code that should be executed and that can cause postgres exceptions.</param> /// <param name="errorType">Type for exception that will be thrown if unknown exception occurres.</param> /// <param name="errorMessage">Message for exception that will be thrown if unknown exception occurres.</param> /// <exception cref="InvalidOperationException">In case when IOE get caught - it will be rethrown.</exception> /// <exception cref="MigrationException">Any exception except IOE will be rethrown as MigrationException.</exception> private async Task TryExecuteAsync(Func <Task> action, MigrationError errorType, string errorMessage) { try { await action.Invoke(); } catch (PostgresException e) when(e.SqlState.StartsWith("08") || e.SqlState == "3D000" || e.SqlState == "3F000") { throw new MigrationException(MigrationError.ConnectionError, $"Can not connect to database \"{DbName}\"", e); } catch (PostgresException e) when(e.SqlState.StartsWith("28") || e.SqlState == "0P000" || e.SqlState == "42501" || e.SqlState == "42000") { throw new MigrationException(MigrationError.AuthorizationError, $"Invalid authorization specification for {DbName}", e); } catch (NpgsqlException e) { throw new MigrationException(MigrationError.MigratingError, $"Error occured while migrating database \"{DbName}\"", e); } catch (InvalidOperationException) { throw; } catch (Exception e) { throw new MigrationException(errorType, errorMessage, e); } }
/// <inheritdoc /> public MigrationException(MigrationError error, string message, Exception inner) : base(message, inner) { Error = error; }
/// <inheritdoc /> public MigrationException(MigrationError error, string message) : base(message) { Error = error; }
/// <inheritdoc /> public MigrationException(MigrationError error) { Error = error; }
/// <summary> /// Create failure migration result with specified params /// </summary> /// <param name="error">Migration error code</param> /// <param name="errorMessage">Error message</param> /// <returns></returns> public static MigrationResult FailureResult(MigrationError error, string errorMessage) { return(new MigrationResult(error, errorMessage)); }