From 78600c9b926d6a865a2e71d268934dddfdf9ee28 Mon Sep 17 00:00:00 2001 From: Yunjin Lee Date: Mon, 24 Apr 2017 14:51:21 +0900 Subject: [PATCH] Update exception handling of Tizen.Security.Privilege Change-Id: Ia240fccb64ab40008a7a99b236ef50cccbb1c3f8 Signed-off-by: Yunjin Lee --- packaging/csapi-security.spec | 2 +- src/Tizen.Security/Interop/Interop.Privilege.cs | 2 + src/Tizen.Security/Tizen.Security/Privilege.cs | 93 +++++++++++++++---------- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/packaging/csapi-security.spec b/packaging/csapi-security.spec index 5b8be54..251d46d 100644 --- a/packaging/csapi-security.spec +++ b/packaging/csapi-security.spec @@ -1,5 +1,5 @@ %define Assemblies Tizen.Security Tizen.Security.SecureRepository -%define version_security 1.0.6 +%define version_security 1.0.7 %define version_secure_repository 1.0.8 Name: csapi-security diff --git a/src/Tizen.Security/Interop/Interop.Privilege.cs b/src/Tizen.Security/Interop/Interop.Privilege.cs index 57f0b83..8b921e8 100755 --- a/src/Tizen.Security/Interop/Interop.Privilege.cs +++ b/src/Tizen.Security/Interop/Interop.Privilege.cs @@ -23,6 +23,8 @@ internal static partial class Interop { internal static partial class Privilege { + internal static string LogTag = "Tizen.Security.Privilege"; + [DllImport(Libraries.Privilege, EntryPoint = "privilege_info_get_display_name")] internal static extern int GetDisplayName(string apiVersion, string privilege, out string displayName); diff --git a/src/Tizen.Security/Tizen.Security/Privilege.cs b/src/Tizen.Security/Tizen.Security/Privilege.cs index 420cb71..a59cfd5 100755 --- a/src/Tizen.Security/Tizen.Security/Privilege.cs +++ b/src/Tizen.Security/Tizen.Security/Privilege.cs @@ -40,7 +40,8 @@ namespace Tizen.Security } else { - throw PrivilegeErrorFactory.GetException(ErrorCode.InvalidParameter, "Invalid Parameter"); + Tizen.Log.Error(Interop.Privilege.LogTag, "Invalid Parameter: PackageType doesn't include TPK or WGT"); + throw new ArgumentException(); } } @@ -51,13 +52,18 @@ namespace Tizen.Security /// The api version /// The privilege /// The display name of given privilege at given api version + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static string GetDisplayName(string apiVersion, string privilege) { string displayName; - int ret = Interop.Privilege.GetDisplayName(apiVersion, privilege, out displayName); - PrivilegeErrorFactory.ThrowException(ret); + if (apiVersion == null || privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetDisplayName(apiVersion, privilege, out displayName), + "Failed to Get Privilege's Display Name."); return displayName; } @@ -69,13 +75,18 @@ namespace Tizen.Security /// The privilege /// The type of application package /// The display name of given privilege at given api version and the package type + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static string GetDisplayName(string apiVersion, string privilege, PackageType packageType) { string displayName; - int ret = Interop.Privilege.GetDisplayNameByPkgtype(ToPackageTypeString(packageType), apiVersion, privilege, out displayName); - PrivilegeErrorFactory.ThrowException(ret); + if (apiVersion == null || privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetDisplayNameByPkgtype(ToPackageTypeString(packageType), apiVersion, privilege, out displayName), + "Failed to Get Privilege's Display Name."); return displayName; } @@ -86,13 +97,18 @@ namespace Tizen.Security /// The api version /// The privilege /// The description of given privilege at given api version + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static string GetDescription(string apiVersion, string privilege) { string description; - int ret = Interop.Privilege.GetDescription(apiVersion, privilege, out description); - PrivilegeErrorFactory.ThrowException(ret); + if (apiVersion == null || privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetDescription(apiVersion, privilege, out description), + "Failed to Get Privilege's Description."); return description; } @@ -104,13 +120,18 @@ namespace Tizen.Security /// The privilege /// The type of application package /// The description of given privilege at given api version and the package type + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static string GetDescription(string apiVersion, string privilege, PackageType packageType) { string description; - int ret = Interop.Privilege.GetDescriptionByPkgtype(ToPackageTypeString(packageType),apiVersion, privilege, out description); - PrivilegeErrorFactory.ThrowException(ret); + if (apiVersion == null || privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "apiVersion and privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetDescriptionByPkgtype(ToPackageTypeString(packageType),apiVersion, privilege, out description), + "Failed to Get Privilege's Description."); return description; } @@ -120,13 +141,18 @@ namespace Tizen.Security /// The privilege /// The privilege must be privacy related. /// The privacy group's display name that the given privilege is included in + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static string GetPrivacyDisplayName(string privilege) { string displayName; - int ret = Interop.Privilege.GetPrivacyDisplayName(privilege, out displayName); - PrivilegeErrorFactory.ThrowException(ret); + if (privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetPrivacyDisplayName(privilege, out displayName), + "Failed to Get Privacy's Display Name in Which the Given Privilege is included."); return displayName; } @@ -136,46 +162,43 @@ namespace Tizen.Security /// The privilege /// The privilege must be privacy related. /// status true if the privilege is on and false if the privilege is off. + /// Thrown when there is a null parameter. /// Thrown when there is an invalid parameter. + /// Thrown when out of memory occurs /// Thrown when internal error occurs. public static bool GetPrivacyPrivilegeStatus(string privilege) { bool status; - int ret = Interop.Privilege.GetPrivacyPrivilegeStatus(privilege, out status); - PrivilegeErrorFactory.ThrowException(ret); + if (privilege == null) + PrivilegeErrorFactory.ThrowException(new ArgumentNullException(), "privilege should not be null."); + PrivilegeErrorFactory.CheckNThrowException( + Interop.Privilege.GetPrivacyPrivilegeStatus(privilege, out status), + "Failed to Get Privacy Privilege's Status."); return status; } } internal static class PrivilegeErrorFactory { - static internal Exception GetException(ErrorCode err, string message) + internal static void ThrowException(Exception e, string msg) { - string errorMessage = string.Format("{0} err = {1}", message, err); - switch (err) - { - case ErrorCode.InvalidParameter: - return new ArgumentException(errorMessage); - case ErrorCode.OutOfMemory: - return new InvalidOperationException(errorMessage); - default: - return new InvalidOperationException(errorMessage); - } + Tizen.Log.Error(Interop.Privilege.LogTag, "[" + e.ToString() + "] " + msg); + throw e; } - - static internal void ThrowException(int error) + internal static void CheckNThrowException(int err, string msg) { - if ((ErrorCode)error == ErrorCode.None) - { + if (err == (int)ErrorCode.None) return; - } - else if ((ErrorCode)error == ErrorCode.InvalidParameter) - { - throw new ArgumentException("Invalid parameter"); - } - else if ((ErrorCode)error == ErrorCode.OutOfMemory) + string errorMessage = string.Format("[{0}] {1}", ErrorFacts.GetErrorMessage(err), msg); + Tizen.Log.Error(Interop.Privilege.LogTag, errorMessage); + switch (err) { - throw new InvalidOperationException("Out of memory"); + case (int)ErrorCode.InvalidParameter: + throw new ArgumentException(); + case (int)ErrorCode.OutOfMemory: + throw new OutOfMemoryException(); + default: + throw new InvalidOperationException(); } } } -- 2.7.4