From 177cb4becf9e964680fb342f3b7a3f2f04073129 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 23 Aug 2017 20:02:54 +0900 Subject: [PATCH] Revise exceptions for MessagePort API Change-Id: Ib72c74380c13a79f3685906bbf80f41a9710737c Signed-off-by: Inkyun Kil --- .../Tizen.Applications.Messages/MessagePort.cs | 31 ++++++++++++++-------- .../MessagePortErrorFactory.cs | 15 ++++++++--- .../Tizen.Applications.Messages/RemotePort.cs | 8 +++--- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs index b429a88..dc44e9d 100755 --- a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePort.cs @@ -52,7 +52,7 @@ namespace Tizen.Applications.Messages /// /// The name of the local message port. /// If true, it is the trusted message port of application, otherwise false. - /// Thrown when portName is null or empty. + /// Thrown when portName is null or empty. /// /// MessagePort messagePort = new MessagePort("SenderPort", true); /// @@ -125,7 +125,9 @@ namespace Tizen.Applications.Messages /// /// Register the local message port. /// - /// Thrown when portName is already used, when there is an invalid parameter, when out of memory, when there is an I/O error. + /// Thrown when portName is already used, when there is an I/O error. + /// Thrown when there is an invalid parameter. + /// Thrown when out of memory. /// /// MessagePort messagePort = new MessagePort("SenderPort", true); /// messagePort.MessageReceived += MessageReceivedCallback; @@ -137,7 +139,7 @@ namespace Tizen.Applications.Messages { if (s_portMap.Contains(_portName)) { - throw new InvalidOperationException(_portName + " is already used"); + MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, _portName + "is already used"); } _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) => { @@ -163,7 +165,7 @@ namespace Tizen.Applications.Messages Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero); if (_portId <= 0) - throw new InvalidOperationException("Can't Listening with " + _portName); + MessagePortErrorFactory.ThrowException(_portId, "RegisterPort", _portName); s_portMap.Add(_portName); _listening = true; @@ -173,7 +175,9 @@ namespace Tizen.Applications.Messages /// /// Unregisters the local message port. /// - /// Thrown when messageport is already stopped, when there is an invalid parameter, when the port is not found, when out of memory, when there is an I/O error. + /// Thrown when messageport is already stopped, when there is an I/O error, when the port is not found. + /// Thrown when there is an invalid parameter. + /// Thrown when out of memory. /// /// MessagePort messagePort = new MessagePort("SenderPort", true); /// messagePort.MessageReceived += MessageReceivedCallback; @@ -189,7 +193,7 @@ namespace Tizen.Applications.Messages { if (!_listening) { - throw new InvalidOperationException("Already stopped"); + MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped"); } int ret = _trusted ? @@ -198,7 +202,7 @@ namespace Tizen.Applications.Messages if (ret != (int)MessagePortError.None) { - MessagePortErrorFactory.ThrowException(ret); + MessagePortErrorFactory.ThrowException(ret, "Error Unregister port"); } lock (s_lock) @@ -215,7 +219,9 @@ namespace Tizen.Applications.Messages /// The message to be passed to the remote application, the recommended message size is under 4KB. /// The ID of the remote application. /// The name of the remote message port. - /// Thrown when there is an invalid parameter, when the port is not found, when out of memory, when there is an I/O error. + /// Thrown when there is an I/O error, when the port is not found. + /// Thrown when there is an invalid parameter. + /// Thrown when out of memory. /// Thrown when message has exceeded the maximum limit(4KB). /// /// MessagePort messagePort = new MessagePort("SenderPort", true); @@ -239,8 +245,11 @@ namespace Tizen.Applications.Messages /// The ID of the remote application. /// The name of the remote message port. /// If true, it is the trusted message port of remote application, otherwise false. - /// Thrown when there is an invalid parameter, when the port is not found, when out of memory, when there is an I/O error. + /// Thrown when there is an I/O error, when the port is not found. + /// Thrown when there is an invalid parameter. + /// Thrown when out of memory. /// Thrown when message has exceeded the maximum limit(4KB). + /// Thrown when the remote application is not signed with the same certificate. /// /// MessagePort messagePort = new MessagePort("SenderPort", true); /// messagePort.MessageReceived += MessageReceivedCallback; @@ -255,11 +264,11 @@ namespace Tizen.Applications.Messages { if (!_listening) { - throw new InvalidOperationException("Should start listen before send"); + MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Should start listen before send"); } if (message == null) { - throw new ArgumentNullException("message"); + MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "message is null", "Message"); } int ret = trusted ? Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId) : diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePortErrorFactory.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePortErrorFactory.cs index 949d067..5215ac1 100755 --- a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePortErrorFactory.cs +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/MessagePortErrorFactory.cs @@ -15,6 +15,7 @@ */ using System; +using System.Runtime.CompilerServices; using Tizen.Internals.Errors; namespace Tizen.Applications.Messages @@ -34,22 +35,28 @@ namespace Tizen.Applications.Messages internal static class MessagePortErrorFactory { - internal static void ThrowException(int errorCode, string errorMessage = null, string paramName = null) + private const string LogTag = "Tizen.Applications.MessagePort"; + internal static void ThrowException(int errorCode, string errorMessage = null, string paramName = null, + [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0) { MessagePortError err = (MessagePortError)errorCode; if (String.IsNullOrEmpty(errorMessage)) { errorMessage = err.ToString(); } + + Log.Error(LogTag, $"{memberName}({lineNumber.ToString()}) : {filePath}"); + Log.Error(LogTag, "Error : " + errorMessage); + switch ((MessagePortError)errorCode) { case MessagePortError.IOError: - case MessagePortError.OutOfMemory: case MessagePortError.InvalidOperation: case MessagePortError.PortNotFound: case MessagePortError.ResourceUnavailable: throw new InvalidOperationException(errorMessage); - case MessagePortError.InvalidParameter: - case MessagePortError.CertificateNotMatch: throw new ArgumentException(errorMessage, paramName); + case MessagePortError.InvalidParameter: throw new ArgumentException(errorMessage, paramName); + case MessagePortError.CertificateNotMatch: throw new UnauthorizedAccessException(errorMessage); + case MessagePortError.OutOfMemory: throw new OutOfMemoryException(errorMessage); case MessagePortError.MaxExceeded: throw new ArgumentOutOfRangeException(paramName, errorMessage); } } diff --git a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs index 1d6d0d4..5e9f541 100755 --- a/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs +++ b/src/Tizen.Applications.MessagePort/Tizen.Applications.Messages/RemotePort.cs @@ -47,7 +47,7 @@ namespace Tizen.Applications.Messages /// The Id of the remote application /// The name of the remote message port /// If true is the trusted message port of application, otherwise false - /// Thrown when appId is null or empty, when portName is null or empty + /// Thrown when appId is null or empty, when portName is null or empty /// /// RemotePort remotePort = new RemotePort("org.tizen.example.messageport", "SenderPort", false); /// @@ -135,7 +135,8 @@ namespace Tizen.Applications.Messages /// Check if the remote message port is running. /// /// 4 - /// Thrown when out of memory, when there is an I/O error + /// Thrown when there is an I/O error + /// Thrown when out of memory. /// /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true); /// bool isRunning = remotePort.isRunning(); @@ -161,7 +162,8 @@ namespace Tizen.Applications.Messages /// Called when the remote port is registered or unregistered. /// /// 4 - /// Thrown when out of memory, when there is an I/O error + /// Thrown when there is an I/O error + /// Thrown when out of memory. /// /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true); /// remotePort.RemotePortStateChanged += RemotePortStateChangedCallback; -- 2.7.4