private static void wrapIOException( IOException e ) throws CommandFailed { throw new CommandFailed( format( "unable to dump database: %s: %s", e.getClass().getSimpleName(), e.getMessage() ), e ); }
@Test void shouldThrowIfTheArchiveFormatIsInvalid() throws IOException, IncorrectFormat { doThrow( IncorrectFormat.class ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertThat( commandFailed.getMessage(), containsString( archive.toString() ) ); assertThat( commandFailed.getMessage(), containsString( "valid Neo4j archive" ) ); }
private void commandFailed( CommandFailed e ) { failure( "command failed", e, e.code() ); }
public static void wrapIOException( IOException e ) throws CommandFailed { throw new CommandFailed( format( "unable to load database: %s: %s", e.getClass().getSimpleName(), e.getMessage() ), e ); }
@Test void shouldWrapIOExceptionsCarefullyBecauseCriticalInformationIsOftenEncodedInTheirNameButMissingFromTheirMessage() throws Exception { doThrow( new IOException( "the-message" ) ).when( dumper ).dump(any(), any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertEquals( "unable to dump database: IOException: the-message", commandFailed.getMessage() ); }
private static long parsePid( Args args ) throws CommandFailed { if ( args.has( PID_KEY ) ) { try { return Long.parseLong( args.get( PID_KEY, "" ) ); } catch ( NumberFormatException e ) { throw new CommandFailed( "Unable to parse --" + PID_KEY, e ); } } return NO_PID; }
@Test void databaseThatRequireRecoveryIsNotDumpable() throws IOException { File logFile = new File( databaseDirectory.toFile(), TransactionLogFiles.DEFAULT_NAME + ".0" ); try ( FileWriter fileWriter = new FileWriter( logFile ) ) { fileWriter.write( "brb" ); } CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertThat( commandFailed.getMessage(), startsWith( "Active logical log detected, this might be a source of inconsistencies." ) ); }
throw new CommandFailed( "archive does not exist: " + archive, e ); throw new CommandFailed( "database already exists: " + database, e ); throw new CommandFailed( "you do not have permission to load a database -- is Neo4j running as a " + "different user?", e ); throw new CommandFailed( "Not a valid Neo4j archive: " + archive, incorrectFormat );
@Test void shouldGiveAClearMessageIfTheArchivesParentDoesntExist() throws Exception { doThrow( new NoSuchFileException( archive.getParent().toString() ) ).when( dumper ).dump(any(), any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertEquals( "unable to dump database: NoSuchFileException: " + archive.getParent(), commandFailed.getMessage() ); }
@Override public void execute( String[] args ) throws IncorrectUsage, CommandFailed { try { setPassword( arguments.parse( args ).get( 0 ) ); } catch ( IncorrectUsage | CommandFailed e ) { throw e; } catch ( Throwable throwable ) { throw new CommandFailed( throwable.getMessage(), new RuntimeException( throwable ) ); } }
@Test void shouldGiveAClearMessageIfTheDatabaseAlreadyExists() throws IOException, IncorrectFormat, IncorrectUsage { doThrow( FileAlreadyExistsException.class ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertEquals( "database already exists: foo.db", commandFailed.getMessage() ); }
@Override public void execute( String[] args ) throws IncorrectUsage, CommandFailed { try { setDefaultAdmin( arguments.parse( args ).get( 0 ) ); } catch ( IncorrectUsage | CommandFailed e ) { throw e; } catch ( Throwable throwable ) { throw new CommandFailed( throwable.getMessage(), new RuntimeException( throwable ) ); } }
@Test void shouldGiveAClearMessageIfTheDatabasesDirectoryIsNotWritable() throws IOException, IncorrectFormat { doThrow( AccessDeniedException.class ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertEquals( "you do not have permission to load a database -- is Neo4j running as a different user?", commandFailed.getMessage() ); }
private Config getConfig( File configFile ) throws CommandFailed { if ( !fs.fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }
@Test void shouldGiveAClearErrorIfTheArchiveAlreadyExists() throws Exception { doThrow( new FileAlreadyExistsException( "the-archive-path" ) ).when( dumper ).dump( any(), any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( "foo.db" ) ); assertEquals( "archive already exists: the-archive-path", commandFailed.getMessage() ); }
Importer getImporterForMode( String mode, Args parsedArgs, Config config, OutsideWorld outsideWorld ) throws IncorrectUsage, CommandFailed { Importer importer; switch ( mode ) { case "database": importer = new DatabaseImporter( parsedArgs, config, outsideWorld ); break; case "csv": importer = new CsvImporter( parsedArgs, config, outsideWorld ); break; default: throw new CommandFailed( "Invalid mode specified." ); // This won't happen because mode is mandatory. } return importer; } }
@Test void shouldGiveAClearMessageIfTheArchiveDoesntExist() throws IOException, IncorrectFormat { doThrow( new NoSuchFileException( archive.toString() ) ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertEquals( "archive does not exist: " + archive, commandFailed.getMessage() ); }
private void dump( String database, DatabaseLayout databaseLayout, Path transactionalLogsDirectory, Path archive ) throws CommandFailed { Path databasePath = databaseLayout.databaseDirectory().toPath(); try { File storeLockFile = databaseLayout.getStoreLayout().storeLockFile(); dumper.dump( databasePath, transactionalLogsDirectory, archive, path -> Objects.equals( path.getFileName().toString(), storeLockFile.getName() ) ); } catch ( FileAlreadyExistsException e ) { throw new CommandFailed( "archive already exists: " + e.getMessage(), e ); } catch ( NoSuchFileException e ) { if ( Paths.get( e.getMessage() ).toAbsolutePath().equals( databasePath ) ) { throw new CommandFailed( "database does not exist: " + database, e ); } wrapIOException( e ); } catch ( IOException e ) { wrapIOException( e ); } }
@Test void shouldWrapIOExceptionsCarefullyBecauseCriticalInformationIsOftenEncodedInTheirNameButMissingFromTheirMessage() throws IOException, IncorrectFormat { doThrow( new FileSystemException( "the-message" ) ).when( loader ).load( any(), any(), any() ); CommandFailed commandFailed = assertThrows( CommandFailed.class, () -> execute( null ) ); assertEquals( "unable to load database: FileSystemException: the-message", commandFailed.getMessage() ); }
private Config getConfig( File configFile, String databaseName ) throws CommandFailed { if ( !outsideWorld.fileSystem().fileExists( configFile ) ) { throw new CommandFailed( "Unable to find config file, tried: " + configFile.getAbsolutePath() ); } try { return Config.fromFile( configFile ).withHome( homeDir ).withSetting( active_database, databaseName ).withConnectorsDisabled().build(); } catch ( Exception e ) { throw new CommandFailed( "Failed to read config file: " + configFile.getAbsolutePath(), e ); } }