Просмотр исходного кода

Make haxe.Error more generic (#9005)

* free haxe.Error from asys dependency

* remove type param from haxe.Error
Aleksandr Kuzmenko 5 лет назад
Родитель
Сommit
b832af987b

+ 0 - 1
std/asys/AsyncFileSystem.hx

@@ -1,6 +1,5 @@
 package asys;
 
-import haxe.Error;
 import haxe.NoData;
 import haxe.async.Callback;
 import haxe.io.Bytes;

+ 0 - 1
std/asys/FileSystem.hx

@@ -1,6 +1,5 @@
 package asys;
 
-import haxe.Error;
 import haxe.io.Bytes;
 import haxe.io.FilePath;
 import asys.io.*;

+ 2 - 2
std/asys/FileWatcher.hx

@@ -1,6 +1,6 @@
 package asys;
 
-import haxe.Error;
+import asys.uv.UVError;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.FilePath;
@@ -25,7 +25,7 @@ class FileWatcher {
 	/**
 		Emitted when an error occurs.
 	**/
-	public final errorSignal:Signal<Error> = new ArraySignal();
+	public final errorSignal:Signal<UVError> = new ArraySignal();
 
 	private var native:FileWatcherNative;
 

+ 2 - 2
std/asys/Process.hx

@@ -1,6 +1,6 @@
 package asys;
 
-import haxe.Error;
+import asys.uv.UVError;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.*;
@@ -96,7 +96,7 @@ class Process {
 	/**
 		Emitted when an error occurs during communication with `this` process.
 	**/
-	public final errorSignal:Signal<Error> = new ArraySignal();
+	public final errorSignal:Signal<UVError> = new ArraySignal();
 
 	/**
 		Emitted when `this` process exits, potentially due to a signal.

+ 0 - 1
std/asys/io/AsyncFile.hx

@@ -1,6 +1,5 @@
 package asys.io;
 
-import haxe.Error;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.Bytes;

+ 0 - 1
std/asys/io/File.hx

@@ -1,6 +1,5 @@
 package asys.io;
 
-import haxe.Error;
 import haxe.io.Bytes;
 import haxe.io.Encoding;
 import asys.*;

+ 0 - 1
std/asys/io/IpcSerializer.hx

@@ -1,6 +1,5 @@
 package asys.io;
 
-import haxe.Error;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.*;

+ 0 - 1
std/asys/io/IpcUnserializer.hx

@@ -1,6 +1,5 @@
 package asys.io;
 
-import haxe.Error;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.*;

+ 2 - 2
std/asys/net/Server.hx

@@ -1,6 +1,6 @@
 package asys.net;
 
-import haxe.Error;
+import asys.uv.UVError;
 import haxe.NoData;
 import haxe.async.*;
 
@@ -29,7 +29,7 @@ typedef ServerListenIpcOptions = {
 class Server {
 	public final closeSignal:Signal<NoData> = new ArraySignal<NoData>();
 	public final connectionSignal:Signal<Socket> = new ArraySignal<Socket>();
-	public final errorSignal:Signal<Error> = new ArraySignal<Error>();
+	public final errorSignal:Signal<UVError> = new ArraySignal<UVError>();
 	public final listeningSignal:Signal<NoData> = new ArraySignal<NoData>();
 
 	public var listening(default, null):Bool;

+ 2 - 2
std/asys/net/Socket.hx

@@ -1,6 +1,6 @@
 package asys.net;
 
-import haxe.Error;
+import asys.uv.UVError;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.*;
@@ -196,7 +196,7 @@ class Socket extends Duplex {
 
 	// TODO: keep track of pending writes for finish event emission
 	// in `internalWrite` and `writeHandle`
-	function writeDone(err:Error, nd:NoData):Void {
+	function writeDone(err:UVError, nd:NoData):Void {
 		timeoutReset();
 		if (err != null)
 			errorSignal.emit(err);

+ 2 - 2
std/asys/net/UdpSocket.hx

@@ -1,6 +1,6 @@
 package asys.net;
 
-import haxe.Error;
+import asys.uv.UVError;
 import haxe.NoData;
 import haxe.async.*;
 import haxe.io.Bytes;
@@ -49,7 +49,7 @@ class UdpSocket {
 	// final connectSignal:Signal<NoData>;
 	// final listeningSignal:Signal<NoData>;
 
-	public final errorSignal:Signal<Error> = new ArraySignal();
+	public final errorSignal:Signal<UVError> = new ArraySignal();
 
 	/**
 		Emitted when a message is received by `this` socket. See `UdpMessage`.

+ 17 - 0
std/asys/uv/UVError.hx

@@ -0,0 +1,17 @@
+package asys.uv;
+
+import haxe.PosInfos;
+import asys.uv.UVErrorType;
+
+/**
+	LibUV errors wrapper.
+**/
+class UVError extends haxe.Error {
+
+	public final type:UVErrorType;
+
+	public function new(type:UVErrorType, ?p:PosInfos) {
+		super(type, p);
+		this.type = type;
+	}
+}

+ 78 - 385
std/asys/uv/UVErrorType.hx

@@ -1,388 +1,81 @@
 package asys.uv;
 
-extern enum abstract UVErrorType(Int) {
-	/**
-		Argument list too long.
-	**/
-	var E2BIG;
-
-	/**
-		Permission denied.
-	**/
-	var EACCES;
-
-	/**
-		Address already in use.
-	**/
-	var EADDRINUSE;
-
-	/**
-		Address not available.
-	**/
-	var EADDRNOTAVAIL;
-
-	/**
-		Address family not supported.
-	**/
-	var EAFNOSUPPORT;
-
-	/**
-		Resource temporarily unavailable.
-	**/
-	var EAGAIN;
-
-	/**
-		Address family not supported.
-	**/
-	var EAI_ADDRFAMILY;
-
-	/**
-		Temporary failure.
-	**/
-	var EAI_AGAIN;
-
-	/**
-		Bad ai_flags value.
-	**/
-	var EAI_BADFLAGS;
-
-	/**
-		Invalid value for hints.
-	**/
-	var EAI_BADHINTS;
-
-	/**
-		Request canceled.
-	**/
-	var EAI_CANCELED;
-
-	/**
-		Permanent failure.
-	**/
-	var EAI_FAIL;
-
-	/**
-		Ai_family not supported.
-	**/
-	var EAI_FAMILY;
-
-	/**
-		Out of memory.
-	**/
-	var EAI_MEMORY;
-
-	/**
-		No address.
-	**/
-	var EAI_NODATA;
-
-	/**
-		Unknown node or service.
-	**/
-	var EAI_NONAME;
-
-	/**
-		Argument buffer overflow.
-	**/
-	var EAI_OVERFLOW;
-
-	/**
-		Resolved protocol is unknown.
-	**/
-	var EAI_PROTOCOL;
-
-	/**
-		Service not available for socket type.
-	**/
-	var EAI_SERVICE;
-
-	/**
-		Socket type not supported.
-	**/
-	var EAI_SOCKTYPE;
-
-	/**
-		Connection already in progress.
-	**/
-	var EALREADY;
-
-	/**
-		Bad file descriptor.
-	**/
-	var EBADF;
-
-	/**
-		Resource busy or locked.
-	**/
-	var EBUSY;
-
-	/**
-		Operation canceled.
-	**/
-	var ECANCELED;
-
-	/**
-		Invalid Unicode character.
-	**/
-	var ECHARSET;
-
-	/**
-		Software caused connection abort.
-	**/
-	var ECONNABORTED;
-
-	/**
-		Connection refused.
-	**/
-	var ECONNREFUSED;
-
-	/**
-		Connection reset by peer.
-	**/
-	var ECONNRESET;
-
-	/**
-		Destination address required.
-	**/
-	var EDESTADDRREQ;
-
-	/**
-		File already exists.
-	**/
-	var EEXIST;
-
-	/**
-		Bad address in system call argument.
-	**/
-	var EFAULT;
-
-	/**
-		File too large.
-	**/
-	var EFBIG;
-
-	/**
-		Host is unreachable.
-	**/
-	var EHOSTUNREACH;
-
-	/**
-		Interrupted system call.
-	**/
-	var EINTR;
-
-	/**
-		Invalid argument.
-	**/
-	var EINVAL;
-
-	/**
-		I/o error.
-	**/
-	var EIO;
-
-	/**
-		Socket is already connected.
-	**/
-	var EISCONN;
-
-	/**
-		Illegal operation on a directory.
-	**/
-	var EISDIR;
-
-	/**
-		Too many symbolic links encountered.
-	**/
-	var ELOOP;
-
-	/**
-		Too many open files.
-	**/
-	var EMFILE;
-
-	/**
-		Message too long.
-	**/
-	var EMSGSIZE;
-
-	/**
-		Name too long.
-	**/
-	var ENAMETOOLONG;
-
-	/**
-		Network is down.
-	**/
-	var ENETDOWN;
-
-	/**
-		Network is unreachable.
-	**/
-	var ENETUNREACH;
-
-	/**
-		File table overflow.
-	**/
-	var ENFILE;
-
-	/**
-		No buffer space available.
-	**/
-	var ENOBUFS;
-
-	/**
-		No such device.
-	**/
-	var ENODEV;
-
-	/**
-		No such file or directory.
-	**/
-	var ENOENT;
-
-	/**
-		Not enough memory.
-	**/
-	var ENOMEM;
-
-	/**
-		Machine is not on the network.
-	**/
-	var ENONET;
-
-	/**
-		Protocol not available.
-	**/
-	var ENOPROTOOPT;
-
-	/**
-		No space left on device.
-	**/
-	var ENOSPC;
-
-	/**
-		Function not implemented.
-	**/
-	var ENOSYS;
-
-	/**
-		Socket is not connected.
-	**/
-	var ENOTCONN;
-
-	/**
-		Not a directory.
-	**/
-	var ENOTDIR;
-
-	/**
-		Directory not empty.
-	**/
-	var ENOTEMPTY;
-
-	/**
-		Socket operation on non-socket.
-	**/
-	var ENOTSOCK;
-
-	/**
-		Operation not supported on socket.
-	**/
-	var ENOTSUP;
-
-	/**
-		Operation not permitted.
-	**/
-	var EPERM;
-
-	/**
-		Broken pipe.
-	**/
-	var EPIPE;
-
-	/**
-		Protocol error.
-	**/
-	var EPROTO;
-
-	/**
-		Protocol not supported.
-	**/
-	var EPROTONOSUPPORT;
-
-	/**
-		Protocol wrong type for socket.
-	**/
-	var EPROTOTYPE;
-
-	/**
-		Result too large.
-	**/
-	var ERANGE;
-
-	/**
-		Read-only file system.
-	**/
-	var EROFS;
-
-	/**
-		Cannot send after transport endpoint shutdown.
-	**/
-	var ESHUTDOWN;
-
-	/**
-		Invalid seek.
-	**/
-	var ESPIPE;
-
-	/**
-		No such process.
-	**/
-	var ESRCH;
-
-	/**
-		Connection timed out.
-	**/
-	var ETIMEDOUT;
-
-	/**
-		Text file is busy.
-	**/
-	var ETXTBSY;
-
-	/**
-		Cross-device link not permitted.
-	**/
-	var EXDEV;
-
-	/**
-		Unknown error.
-	**/
-	var UNKNOWN;
-
-	/**
-		End of file.
-	**/
-	var EOF;
-
-	/**
-		No such device or address.
-	**/
-	var ENXIO;
-
-	/**
-		Too many links.
-	**/
-	var EMLINK;
-
-	/**
-		Host is down.
-	**/
-	var EHOSTDOWN;
-
-	/**
-		Unknown error within libuv or libuv glue code.
-	**/
-	var EOTHER;
+extern enum abstract UVErrorType(String) to String {
+	var E2BIG = 'Argument list too long';
+	var EACCES = 'Permission denied';
+	var EADDRINUSE = 'Address already in use';
+	var EADDRNOTAVAIL = 'Address not available';
+	var EAFNOSUPPORT = 'Address family not supported';
+	var EAGAIN = 'Resource temporarily unavailable';
+	var EAI_ADDRFAMILY = 'Address family not supported';
+	var EAI_AGAIN = 'Temporary failure';
+	var EAI_BADFLAGS = 'Bad ai_flags value';
+	var EAI_BADHINTS = 'Invalid value for hints';
+	var EAI_CANCELED = 'Request canceled';
+	var EAI_FAIL = 'Permanent failure';
+	var EAI_FAMILY = 'Ai_family not supported';
+	var EAI_MEMORY = 'Out of memory';
+	var EAI_NODATA = 'No address';
+	var EAI_NONAME = 'Unknown node or service';
+	var EAI_OVERFLOW = 'Argument buffer overflow';
+	var EAI_PROTOCOL = 'Resolved protocol is unknown';
+	var EAI_SERVICE = 'Service not available for socket type';
+	var EAI_SOCKTYPE = 'Socket type not supported';
+	var EALREADY = 'Connection already in progress';
+	var EBADF = 'Bad file descriptor';
+	var EBUSY = 'Resource busy or locked';
+	var ECANCELED = 'Operation canceled';
+	var ECHARSET = 'Invalid Unicode character';
+	var ECONNABORTED = 'Software caused connection abort';
+	var ECONNREFUSED = 'Connection refused';
+	var ECONNRESET = 'Connection reset by peer';
+	var EDESTADDRREQ = 'Destination address required';
+	var EEXIST = 'File already exists';
+	var EFAULT = 'Bad address in system call argument';
+	var EFBIG = 'File too large';
+	var EHOSTUNREACH = 'Host is unreachable';
+	var EINTR = 'Interrupted system call';
+	var EINVAL = 'Invalid argument';
+	var EIO = 'I/o error';
+	var EISCONN = 'Socket is already connected';
+	var EISDIR = 'Illegal operation on a directory';
+	var ELOOP = 'Too many symbolic links encountered';
+	var EMFILE = 'Too many open files';
+	var EMSGSIZE = 'Message too long';
+	var ENAMETOOLONG = 'Name too long';
+	var ENETDOWN = 'Network is down';
+	var ENETUNREACH = 'Network is unreachable';
+	var ENFILE = 'File table overflow';
+	var ENOBUFS = 'No buffer space available';
+	var ENODEV = 'No such device';
+	var ENOENT = 'No such file or directory';
+	var ENOMEM = 'Not enough memory';
+	var ENONET = 'Machine is not on the network';
+	var ENOPROTOOPT = 'Protocol not available';
+	var ENOSPC = 'No space left on device';
+	var ENOSYS = 'Function not implemented';
+	var ENOTCONN = 'Socket is not connected';
+	var ENOTDIR = 'Not a directory';
+	var ENOTEMPTY = 'Directory not empty';
+	var ENOTSOCK = 'Socket operation on non-socket';
+	var ENOTSUP = 'Operation not supported on socket';
+	var EPERM = 'Operation not permitted';
+	var EPIPE = 'Broken pipe';
+	var EPROTO = 'Protocol error';
+	var EPROTONOSUPPORT = 'Protocol not supported';
+	var EPROTOTYPE = 'Protocol wrong type for socket';
+	var ERANGE = 'Result too large';
+	var EROFS = 'Read-only file system';
+	var ESHUTDOWN = 'Cannot send after transport endpoint shutdown';
+	var ESPIPE = 'Invalid seek';
+	var ESRCH = 'No such process';
+	var ETIMEDOUT = 'Connection timed out';
+	var ETXTBSY = 'Text file is busy';
+	var EXDEV = 'Cross-device link not permitted';
+	var UNKNOWN = 'Unknown error';
+	var EOF = 'End of file';
+	var ENXIO = 'No such device or address';
+	var EMLINK = 'Too many links';
+	var EHOSTDOWN = 'Host is down';
+	var EOTHER = 'Unknown error within libuv or libuv glue code';
 }

+ 3 - 92
std/haxe/Error.hx

@@ -1,112 +1,23 @@
 package haxe;
 
-import asys.uv.UVErrorType;
 import haxe.PosInfos;
 
 /**
 	Common class for errors.
 **/
 class Error {
-	function get_message():String {
-		return (switch (type) {
-			case UVError(UVErrorType.E2BIG): "argument list too long";
-			case UVError(UVErrorType.EACCES): "permission denied";
-			case UVError(UVErrorType.EADDRINUSE): "address already in use";
-			case UVError(UVErrorType.EADDRNOTAVAIL): "address not available";
-			case UVError(UVErrorType.EAFNOSUPPORT): "address family not supported";
-			case UVError(UVErrorType.EAGAIN): "resource temporarily unavailable";
-			case UVError(UVErrorType.EAI_ADDRFAMILY): "address family not supported";
-			case UVError(UVErrorType.EAI_AGAIN): "temporary failure";
-			case UVError(UVErrorType.EAI_BADFLAGS): "bad ai_flags value";
-			case UVError(UVErrorType.EAI_BADHINTS): "invalid value for hints";
-			case UVError(UVErrorType.EAI_CANCELED): "request canceled";
-			case UVError(UVErrorType.EAI_FAIL): "permanent failure";
-			case UVError(UVErrorType.EAI_FAMILY): "ai_family not supported";
-			case UVError(UVErrorType.EAI_MEMORY): "out of memory";
-			case UVError(UVErrorType.EAI_NODATA): "no address";
-			case UVError(UVErrorType.EAI_NONAME): "unknown node or service";
-			case UVError(UVErrorType.EAI_OVERFLOW): "argument buffer overflow";
-			case UVError(UVErrorType.EAI_PROTOCOL): "resolved protocol is unknown";
-			case UVError(UVErrorType.EAI_SERVICE): "service not available for socket type";
-			case UVError(UVErrorType.EAI_SOCKTYPE): "socket type not supported";
-			case UVError(UVErrorType.EALREADY): "connection already in progress";
-			case UVError(UVErrorType.EBADF): "bad file descriptor";
-			case UVError(UVErrorType.EBUSY): "resource busy or locked";
-			case UVError(UVErrorType.ECANCELED): "operation canceled";
-			case UVError(UVErrorType.ECHARSET): "invalid Unicode character";
-			case UVError(UVErrorType.ECONNABORTED): "software caused connection abort";
-			case UVError(UVErrorType.ECONNREFUSED): "connection refused";
-			case UVError(UVErrorType.ECONNRESET): "connection reset by peer";
-			case UVError(UVErrorType.EDESTADDRREQ): "destination address required";
-			case UVError(UVErrorType.EEXIST): "file already exists";
-			case UVError(UVErrorType.EFAULT): "bad address in system call argument";
-			case UVError(UVErrorType.EFBIG): "file too large";
-			case UVError(UVErrorType.EHOSTUNREACH): "host is unreachable";
-			case UVError(UVErrorType.EINTR): "interrupted system call";
-			case UVError(UVErrorType.EINVAL): "invalid argument";
-			case UVError(UVErrorType.EIO): "i/o error";
-			case UVError(UVErrorType.EISCONN): "socket is already connected";
-			case UVError(UVErrorType.EISDIR): "illegal operation on a directory";
-			case UVError(UVErrorType.ELOOP): "too many symbolic links encountered";
-			case UVError(UVErrorType.EMFILE): "too many open files";
-			case UVError(UVErrorType.EMSGSIZE): "message too long";
-			case UVError(UVErrorType.ENAMETOOLONG): "name too long";
-			case UVError(UVErrorType.ENETDOWN): "network is down";
-			case UVError(UVErrorType.ENETUNREACH): "network is unreachable";
-			case UVError(UVErrorType.ENFILE): "file table overflow";
-			case UVError(UVErrorType.ENOBUFS): "no buffer space available";
-			case UVError(UVErrorType.ENODEV): "no such device";
-			case UVError(UVErrorType.ENOENT): "no such file or directory";
-			case UVError(UVErrorType.ENOMEM): "not enough memory";
-			case UVError(UVErrorType.ENONET): "machine is not on the network";
-			case UVError(UVErrorType.ENOPROTOOPT): "protocol not available";
-			case UVError(UVErrorType.ENOSPC): "no space left on device";
-			case UVError(UVErrorType.ENOSYS): "function not implemented";
-			case UVError(UVErrorType.ENOTCONN): "socket is not connected";
-			case UVError(UVErrorType.ENOTDIR): "not a directory";
-			case UVError(UVErrorType.ENOTEMPTY): "directory not empty";
-			case UVError(UVErrorType.ENOTSOCK): "socket operation on non-socket";
-			case UVError(UVErrorType.ENOTSUP): "operation not supported on socket";
-			case UVError(UVErrorType.EPERM): "operation not permitted";
-			case UVError(UVErrorType.EPIPE): "broken pipe";
-			case UVError(UVErrorType.EPROTO): "protocol error";
-			case UVError(UVErrorType.EPROTONOSUPPORT): "protocol not supported";
-			case UVError(UVErrorType.EPROTOTYPE): "protocol wrong type for socket";
-			case UVError(UVErrorType.ERANGE): "result too large";
-			case UVError(UVErrorType.EROFS): "read-only file system";
-			case UVError(UVErrorType.ESHUTDOWN): "cannot send after transport endpoint shutdown";
-			case UVError(UVErrorType.ESPIPE): "invalid seek";
-			case UVError(UVErrorType.ESRCH): "no such process";
-			case UVError(UVErrorType.ETIMEDOUT): "connection timed out";
-			case UVError(UVErrorType.ETXTBSY): "text file is busy";
-			case UVError(UVErrorType.EXDEV): "cross-device link not permitted";
-			case UVError(UVErrorType.UNKNOWN): "unknown error";
-			case UVError(UVErrorType.EOF): "end of file";
-			case UVError(UVErrorType.ENXIO): "no such device or address";
-			case UVError(UVErrorType.EMLINK): "too many links";
-			case UVError(UVErrorType.EHOSTDOWN): "host is down";
-			case UVError(UVErrorType.EOTHER): "other UV error";
-			case _: "unknown error";
-		});
-	}
-
 	/**
 		A human-readable representation of the error.
 	**/
-	public var message(get, never):String;
+	public var message(default, null):String;
 
 	/**
 		Position where the error was thrown. By default, this is the place where the error is constructed.
 	**/
 	public final posInfos:PosInfos;
 
-	/**
-		Error type, usable for discerning error types with `switch` statements.
-	**/
-	public final type:ErrorType;
-
-	public function new(type:ErrorType, ?posInfos:PosInfos) {
-		this.type = type;
+	public function new(message:String, ?posInfos:PosInfos) {
+		this.message = message;
 		this.posInfos = posInfos;
 	}
 

+ 0 - 5
std/haxe/ErrorType.hx

@@ -1,5 +0,0 @@
-package haxe;
-
-enum ErrorType {
-	UVError(errno:asys.uv.UVErrorType);
-}

+ 3 - 1
std/haxe/async/Callback.hx

@@ -3,11 +3,13 @@ package haxe.async;
 import haxe.Error;
 import haxe.NoData;
 
+//TODO: add Error type parameter to CallbackData type parameters?
+//this could be useful:
 typedef CallbackData<T> = (?error:Error, ?result:T) -> Void;
 
 /**
 	A callback. All callbacks in the standard library are functions which accept
-	two arguments: an error (`haxe.Error`) and a result (`T`). If error is 
+	two arguments: an error (`haxe.Error`) and a result (`T`). If error is
 	non-`null`, result must be `null`. The callback type is declared in 	`CallbackData`.
 
 	This abstract defines multiple `@:from` conversions to improve readability of