1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-31 13:42:59 +00:00

Provide various error messages for file handles

- fs.open will return an error message if the handle cannot be read
 - fs and http handles will error when reading from a closed file
This commit is contained in:
SquidDev
2017-05-10 10:48:59 +01:00
parent 25128dfb66
commit 2c63a5f9a3
2 changed files with 24 additions and 1 deletions

View File

@@ -292,7 +292,7 @@ public class FSAPI implements ILuaAPI
} }
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
return null; return new Object[] { null, e.getMessage() };
} }
} }
case 12: case 12:
@@ -372,6 +372,8 @@ public class FSAPI implements ILuaAPI
private static Object[] wrapBufferedReader( final IMountedFileNormal reader ) private static Object[] wrapBufferedReader( final IMountedFileNormal reader )
{ {
return new Object[] { new ILuaObject() { return new Object[] { new ILuaObject() {
private boolean open = true;
@Nonnull @Nonnull
@Override @Override
public String[] getMethodNames() public String[] getMethodNames()
@@ -391,6 +393,7 @@ public class FSAPI implements ILuaAPI
case 0: case 0:
{ {
// readLine // readLine
if( !open ) throw new LuaException( "attempt to use a closed file" );
try { try {
String line = reader.readLine(); String line = reader.readLine();
if( line != null ) { if( line != null ) {
@@ -405,6 +408,7 @@ public class FSAPI implements ILuaAPI
case 1: case 1:
{ {
// readAll // readAll
if( !open ) throw new LuaException( "attempt to use a closed file" );
try { try {
StringBuilder result = new StringBuilder( "" ); StringBuilder result = new StringBuilder( "" );
String line = reader.readLine(); String line = reader.readLine();
@@ -425,6 +429,7 @@ public class FSAPI implements ILuaAPI
// close // close
try { try {
reader.close(); reader.close();
open = false;
return null; return null;
} catch( IOException e ) { } catch( IOException e ) {
return null; return null;
@@ -442,6 +447,8 @@ public class FSAPI implements ILuaAPI
private static Object[] wrapBufferedWriter( final IMountedFileNormal writer ) private static Object[] wrapBufferedWriter( final IMountedFileNormal writer )
{ {
return new Object[] { new ILuaObject() { return new Object[] { new ILuaObject() {
private boolean open = true;
@Nonnull @Nonnull
@Override @Override
public String[] getMethodNames() public String[] getMethodNames()
@@ -462,6 +469,7 @@ public class FSAPI implements ILuaAPI
case 0: case 0:
{ {
// write // write
if( !open ) throw new LuaException( "attempt to use a closed file" );
String text; String text;
if( args.length > 0 && args[0] != null ) { if( args.length > 0 && args[0] != null ) {
text = args[0].toString(); text = args[0].toString();
@@ -478,6 +486,8 @@ public class FSAPI implements ILuaAPI
case 1: case 1:
{ {
// writeLine // writeLine
if( !open ) throw new LuaException( "attempt to use a closed file" );
String text; String text;
if( args.length > 0 && args[0] != null ) { if( args.length > 0 && args[0] != null ) {
text = args[0].toString(); text = args[0].toString();
@@ -496,6 +506,7 @@ public class FSAPI implements ILuaAPI
// close // close
try { try {
writer.close(); writer.close();
open = false;
return null; return null;
} catch( IOException e ) { } catch( IOException e ) {
return null; return null;
@@ -504,6 +515,7 @@ public class FSAPI implements ILuaAPI
case 3: case 3:
{ {
try { try {
if( !open ) throw new LuaException( "attempt to use a closed file" );
writer.flush(); writer.flush();
return null; return null;
} catch ( IOException e ) { } catch ( IOException e ) {
@@ -524,6 +536,7 @@ public class FSAPI implements ILuaAPI
{ {
return new Object[] { new ILuaObject() { return new Object[] { new ILuaObject() {
private boolean open = true;
@Nonnull @Nonnull
@Override @Override
@@ -541,6 +554,7 @@ public class FSAPI implements ILuaAPI
case 0: case 0:
{ {
// read // read
if( !open ) throw new LuaException( "attempt to use a closed file" );
try { try {
int b = reader.read(); int b = reader.read();
if( b != -1 ) { if( b != -1 ) {
@@ -557,6 +571,7 @@ public class FSAPI implements ILuaAPI
//close //close
try { try {
reader.close(); reader.close();
open = false;
return null; return null;
} catch( IOException e ) { } catch( IOException e ) {
return null; return null;
@@ -576,6 +591,7 @@ public class FSAPI implements ILuaAPI
{ {
return new Object[] { new ILuaObject() { return new Object[] { new ILuaObject() {
private boolean open = true;
@Nonnull @Nonnull
@Override @Override
@@ -594,6 +610,7 @@ public class FSAPI implements ILuaAPI
case 0: case 0:
{ {
// write // write
if( !open ) throw new LuaException( "attempt to use a closed file" );
try { try {
if( args.length > 0 && args[0] instanceof Number ) if( args.length > 0 && args[0] instanceof Number )
{ {
@@ -610,6 +627,7 @@ public class FSAPI implements ILuaAPI
//close //close
try { try {
writer.close(); writer.close();
open = false;
return null; return null;
} catch( IOException e ) { } catch( IOException e ) {
return null; return null;
@@ -617,6 +635,7 @@ public class FSAPI implements ILuaAPI
} }
case 2: case 2:
{ {
if( !open ) throw new LuaException( "attempt to use a closed file" );
try { try {
writer.flush(); writer.flush();
return null; return null;

View File

@@ -73,6 +73,7 @@ public class HTTPAPI implements ILuaAPI
private static ILuaObject wrapBufferedReader( final BufferedReader reader, final int responseCode, final Map<String, String> responseHeaders ) private static ILuaObject wrapBufferedReader( final BufferedReader reader, final int responseCode, final Map<String, String> responseHeaders )
{ {
return new ILuaObject() { return new ILuaObject() {
private boolean open = true;
@Nonnull @Nonnull
@Override @Override
public String[] getMethodNames() public String[] getMethodNames()
@@ -94,6 +95,7 @@ public class HTTPAPI implements ILuaAPI
case 0: case 0:
{ {
// readLine // readLine
if( !open ) throw new LuaException( "attempt to use a closed response" );
try { try {
String line = reader.readLine(); String line = reader.readLine();
if( line != null ) { if( line != null ) {
@@ -108,6 +110,7 @@ public class HTTPAPI implements ILuaAPI
case 1: case 1:
{ {
// readAll // readAll
if( !open ) throw new LuaException( "attempt to use a closed response" );
try { try {
StringBuilder result = new StringBuilder( "" ); StringBuilder result = new StringBuilder( "" );
String line = reader.readLine(); String line = reader.readLine();
@@ -128,6 +131,7 @@ public class HTTPAPI implements ILuaAPI
// close // close
try { try {
reader.close(); reader.close();
open = false;
return null; return null;
} catch( IOException e ) { } catch( IOException e ) {
return null; return null;