Refactor error handling logic 49/127049/2
authorSeungha Son <seungha.son@samsung.com>
Wed, 26 Apr 2017 04:12:14 +0000 (13:12 +0900)
committerSeungha Son <seungha.son@samsung.com>
Wed, 26 Apr 2017 05:03:05 +0000 (14:03 +0900)
Signed-off-by: Seungha Son <seungha.son@samsung.com>
Change-Id: I3e1a1a7107a063da86fb2d9d41024cfc4962c183

Tizen.Applications.Badge/Interop/Interop.Badge.cs
Tizen.Applications.Badge/Tizen.Applications.Badge.csproj
Tizen.Applications.Badge/Tizen.Applications/BadgeControl.cs
Tizen.Applications.Badge/Tizen.Applications/BadgeErrorFactory.cs [new file with mode: 0755]

index 2de3367..53b1b1a 100755 (executable)
 
 using System;
 using System.Runtime.InteropServices;
+using Tizen.Applications;
 
 internal static partial class Interop
 {
     internal static partial class Badge
     {
-        internal enum ErrorCode : int
-        {
-            None = Tizen.Internals.Errors.ErrorCode.None,
-            InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
-            OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
-            PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
-            IoError = Tizen.Internals.Errors.ErrorCode.IoError,
-            FromDb = -0x01120000 | 0x01,
-            AlreadyExist = -0x01120000 | 0x02,
-            FromDbus = -0x01120000 | 0x03,
-            NotExist = -0x01120000 | 0x04,
-            ServiceNotReady = -0x01120000 | 0x05,
-            InvalidPackage = -0x01120000 | 0x06
-        }
-
         internal enum Action : uint
         {
             Create = 0,
@@ -50,30 +36,30 @@ internal static partial class Interop
         internal delegate void ChangedCallback(Action action, string appId, uint count, IntPtr userData);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_add")]
-        internal static extern ErrorCode Add(string appId);
+        internal static extern BadgeError Add(string appId);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_remove")]
-        internal static extern ErrorCode Remove(string appId);
+        internal static extern BadgeError Remove(string appId);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_set_count")]
-        internal static extern ErrorCode SetCount(string appId, uint count);
+        internal static extern BadgeError SetCount(string appId, uint count);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_get_count")]
-        internal static extern ErrorCode GetCount(string appId, out uint count);
+        internal static extern BadgeError GetCount(string appId, out uint count);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_set_display")]
-        internal static extern ErrorCode SetDisplay(string appId, uint isDisplay);
+        internal static extern BadgeError SetDisplay(string appId, uint isDisplay);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_get_display")]
-        internal static extern ErrorCode GetDisplay(string appId, out uint isDisplay);
+        internal static extern BadgeError GetDisplay(string appId, out uint isDisplay);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_foreach")]
-        internal static extern ErrorCode Foreach(ForeachCallback callback, IntPtr userData);
+        internal static extern BadgeError Foreach(ForeachCallback callback, IntPtr userData);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_register_changed_cb")]
-        internal static extern ErrorCode SetChangedCallback(ChangedCallback callback, IntPtr userData);
+        internal static extern BadgeError SetChangedCallback(ChangedCallback callback, IntPtr userData);
 
         [DllImport(Libraries.Badge, EntryPoint = "badge_unregister_changed_cb")]
-        internal static extern ErrorCode UnsetChangedCallback(ChangedCallback callback);
+        internal static extern BadgeError UnsetChangedCallback(ChangedCallback callback);
     }
 }
index 0d9135c..7e7bfce 100755 (executable)
@@ -1,83 +1,84 @@
-<?xml version="1.0" encoding="utf-8"?>\r
-<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
-  <PropertyGroup>\r
-    <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>\r
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>\r
-    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>\r
-    <ProductVersion>8.0.30703</ProductVersion>\r
-    <SchemaVersion>2.0</SchemaVersion>\r
-    <ProjectGuid>{A667ADA4-E7DB-4E45-9D31-491024750EE1}</ProjectGuid>\r
-    <OutputType>Library</OutputType>\r
-    <AppDesignerFolder>Properties</AppDesignerFolder>\r
-    <RootNamespace>Tizen.Applications.Badge</RootNamespace>\r
-    <AssemblyName>Tizen.Applications.Badge</AssemblyName>\r
-    <FileAlignment>512</FileAlignment>\r
-    <DefaultLanguage>en-US</DefaultLanguage>\r
-  </PropertyGroup>\r
-  <PropertyGroup>\r
-    <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>\r
-    <TargetFrameworkVersion>v1.3</TargetFrameworkVersion>\r
-    <NuGetTargetMoniker>.NETStandard,Version=v1.3</NuGetTargetMoniker>\r
-    <AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>\r
-    <NoStdLib>true</NoStdLib>\r
-    <NoWarn>$(NoWarn);1701</NoWarn>\r
-    <UseVSHostingProcess>false</UseVSHostingProcess>\r
-  </PropertyGroup>\r
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">\r
-    <DebugSymbols>true</DebugSymbols>\r
-    <DebugType>full</DebugType>\r
-    <Optimize>false</Optimize>\r
-    <OutputPath>bin\Debug\</OutputPath>\r
-    <DefineConstants>DEBUG;TRACE</DefineConstants>\r
-    <ErrorReport>prompt</ErrorReport>\r
-    <WarningLevel>4</WarningLevel>\r
-  </PropertyGroup>\r
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">\r
-    <DebugType>pdbonly</DebugType>\r
-    <Optimize>true</Optimize>\r
-    <OutputPath>bin\Release\</OutputPath>\r
-    <DefineConstants>TRACE</DefineConstants>\r
-    <ErrorReport>prompt</ErrorReport>\r
-    <WarningLevel>4</WarningLevel>\r
-  </PropertyGroup>\r
-  <PropertyGroup>\r
-    <SignAssembly>true</SignAssembly>\r
-  </PropertyGroup>\r
-  <PropertyGroup>\r
-    <AssemblyOriginatorKeyFile>Tizen.Applications.Badge.snk</AssemblyOriginatorKeyFile>\r
-  </PropertyGroup>\r
-  <ItemGroup>\r
-    <None Include="Tizen.Applications.Badge.project.json" />\r
-    <None Include="Tizen.Applications.Badge.nuspec" />\r
-    <None Include="Tizen.Applications.Badge.snk" />\r
-  </ItemGroup>\r
-  <ItemGroup>\r
-    <Compile Include="Interop\Interop.Badge.cs" />\r
-    <Compile Include="Interop\Interop.Libraries.cs" />\r
-    <Compile Include="Properties\AssemblyInfo.cs" />\r
-    <Compile Include="Tizen.Applications\Badge.cs" />\r
-    <Compile Include="Tizen.Applications\BadgeControl.cs" />\r
-    <Compile Include="Tizen.Applications\BadgeEventArgs.cs" />\r
-  </ItemGroup>\r
-  <ItemGroup />\r
-  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />\r
-  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.\r
-       Other similar extension points exist, see Microsoft.Common.targets.\r
-  <Target Name="BeforeBuild">\r
-  </Target>\r
-  <Target Name="AfterBuild">\r
-  </Target>\r
-  -->\r
-  <PropertyGroup>\r
-    <!-- https://github.com/dotnet/corefxlab/tree/master/samples/NetCoreSample and\r
-                        https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/target-dotnetcore-with-msbuild\r
-               -->\r
-    <!-- We don't use any of MSBuild's resolution logic for resolving the framework, so just set these two\r
-                        properties to any folder that exists to skip the GetReferenceAssemblyPaths task (not target) and\r
-                        to prevent it from outputting a warning (MSB3644).\r
-               -->\r
-    <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)</_TargetFrameworkDirectories>\r
-    <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)</_FullFrameworkReferenceAssemblyPaths>\r
-    <AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences>\r
-  </PropertyGroup>\r
-</Project>\r
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{A667ADA4-E7DB-4E45-9D31-491024750EE1}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>Tizen.Applications.Badge</RootNamespace>
+    <AssemblyName>Tizen.Applications.Badge</AssemblyName>
+    <FileAlignment>512</FileAlignment>
+    <DefaultLanguage>en-US</DefaultLanguage>
+  </PropertyGroup>
+  <PropertyGroup>
+    <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
+    <TargetFrameworkVersion>v1.3</TargetFrameworkVersion>
+    <NuGetTargetMoniker>.NETStandard,Version=v1.3</NuGetTargetMoniker>
+    <AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>
+    <NoStdLib>true</NoStdLib>
+    <NoWarn>$(NoWarn);1701</NoWarn>
+    <UseVSHostingProcess>false</UseVSHostingProcess>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup>
+    <SignAssembly>true</SignAssembly>
+  </PropertyGroup>
+  <PropertyGroup>
+    <AssemblyOriginatorKeyFile>Tizen.Applications.Badge.snk</AssemblyOriginatorKeyFile>
+  </PropertyGroup>
+  <ItemGroup>
+    <None Include="Tizen.Applications.Badge.project.json" />
+    <None Include="Tizen.Applications.Badge.nuspec" />
+    <None Include="Tizen.Applications.Badge.snk" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Interop\Interop.Badge.cs" />
+    <Compile Include="Interop\Interop.Libraries.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Tizen.Applications\BadgeErrorFactory.cs" />
+    <Compile Include="Tizen.Applications\Badge.cs" />
+    <Compile Include="Tizen.Applications\BadgeControl.cs" />
+    <Compile Include="Tizen.Applications\BadgeEventArgs.cs" />
+  </ItemGroup>
+  <ItemGroup />
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+  <PropertyGroup>
+    <!-- https://github.com/dotnet/corefxlab/tree/master/samples/NetCoreSample and
+                        https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/target-dotnetcore-with-msbuild
+               -->
+    <!-- We don't use any of MSBuild's resolution logic for resolving the framework, so just set these two
+                        properties to any folder that exists to skip the GetReferenceAssemblyPaths task (not target) and
+                        to prevent it from outputting a warning (MSB3644).
+               -->
+    <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory)</_TargetFrameworkDirectories>
+    <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory)</_FullFrameworkReferenceAssemblyPaths>
+    <AutoUnifyAssemblyReferences>true</AutoUnifyAssemblyReferences>
+  </PropertyGroup>
+</Project>
\ No newline at end of file
index 477cba5..c94f243 100755 (executable)
@@ -44,19 +44,12 @@ namespace Tizen.Applications
                         s_callback = new Interop.Badge.ChangedCallback(OnChangedEvent);
                     }
 
-                    Interop.Badge.ErrorCode err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
-
-                    switch (err)
+                    BadgeError err = Interop.Badge.SetChangedCallback(s_callback, IntPtr.Zero);
+                    if (err != BadgeError.None)
                     {
-                        case Interop.Badge.ErrorCode.InvalidParameter:
-                            throw new InvalidOperationException("Invalid parameter at unmanaged code");
-
-                        case Interop.Badge.ErrorCode.PermissionDenied:
-                            throw new UnauthorizedAccessException();
-
-                        case Interop.Badge.ErrorCode.OutOfMemory:
-                            throw new InvalidOperationException("Out-of-memory at unmanaged code");
+                        throw BadgeErrorFactory.GetException(err, "Failed to add event handler");
                     }
+
                     s_registered = true;
                 }
 
@@ -67,18 +60,10 @@ namespace Tizen.Applications
                 s_changed -= value;
                 if (s_changed == null && s_registered)
                 {
-                    Interop.Badge.ErrorCode err = Interop.Badge.UnsetChangedCallback(s_callback);
-
-                    switch (err)
+                    BadgeError err = Interop.Badge.UnsetChangedCallback(s_callback);
+                    if (err != BadgeError.None)
                     {
-                        case Interop.Badge.ErrorCode.InvalidParameter:
-                            throw new InvalidOperationException("Invalid parameter at unmanaged code");
-
-                        case Interop.Badge.ErrorCode.PermissionDenied:
-                            throw new UnauthorizedAccessException();
-
-                        case Interop.Badge.ErrorCode.NotExist:
-                            throw new InvalidOperationException("Not exist");
+                        throw BadgeErrorFactory.GetException(err, "Failed to remove event handler");
                     }
 
                     s_callback = null;
@@ -100,41 +85,16 @@ namespace Tizen.Applications
             uint count;
             uint display;
 
-            Interop.Badge.ErrorCode err = Interop.Badge.GetCount(appId, out count);
-
-            switch (err)
+            BadgeError err = Interop.Badge.GetCount(appId, out count);
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.FromDb:
-                    throw new InvalidOperationException("Error from DB");
-
-                case Interop.Badge.ErrorCode.OutOfMemory:
-                    throw new InvalidOperationException("Out-of-memory at unmanaged code");
+                throw BadgeErrorFactory.GetException(err, "Failed to find badge count of " + appId);
             }
 
             err = Interop.Badge.GetDisplay(appId, out display);
-
-            switch (err)
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.FromDb:
-                    throw new InvalidOperationException("Error from DB");
-
-                case Interop.Badge.ErrorCode.OutOfMemory:
-                    throw new InvalidOperationException("Out-of-memory at unmanaged code");
-
-                case Interop.Badge.ErrorCode.NotExist:
-                    throw new InvalidOperationException("Not exist");
+                throw BadgeErrorFactory.GetException(err, "Failed to find badge display of " + appId);
             }
 
             return new Badge(appId, (int)count, display == 0 ? false : true);
@@ -150,24 +110,10 @@ namespace Tizen.Applications
         /// <privilege>http://tizen.org/privilege/notification</privilege>
         public static void Remove(string appId)
         {
-            Interop.Badge.ErrorCode err = Interop.Badge.Remove(appId);
-
-            switch (err)
+            BadgeError err = Interop.Badge.Remove(appId);
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.IoError:
-                    throw new InvalidOperationException("Error from I/O");
-
-                case Interop.Badge.ErrorCode.ServiceNotReady:
-                    throw new InvalidOperationException("Service is not ready");
-
-                case Interop.Badge.ErrorCode.NotExist:
-                    throw new InvalidOperationException("Not exist");
+                throw BadgeErrorFactory.GetException(err, "Failed to Remove badge of " + appId);
             }
         }
 
@@ -183,24 +129,10 @@ namespace Tizen.Applications
         /// <privilege>http://tizen.org/privilege/notification</privilege>
         public static void Add(string appId, int count = 1, bool isDisplay = true)
         {
-            Interop.Badge.ErrorCode err = Interop.Badge.Add(appId);
-
-            switch (err)
+            BadgeError err = Interop.Badge.Add(appId);
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.IoError:
-                    throw new InvalidOperationException("Error from I/O");
-
-                case Interop.Badge.ErrorCode.ServiceNotReady:
-                    throw new InvalidOperationException("Service is not ready");
-
-                case Interop.Badge.ErrorCode.InvalidPackage:
-                    throw new InvalidOperationException("The caller application is not signed with the certificate of badge application ID");
+                throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + appId);
             }
 
             try
@@ -225,21 +157,10 @@ namespace Tizen.Applications
         /// <privilege>http://tizen.org/privilege/notification</privilege>
         public static void Update(string appId, int count)
         {
-            Interop.Badge.ErrorCode err = Interop.Badge.SetCount(appId, (uint)count);
-
-            switch (err)
+            BadgeError err = Interop.Badge.SetCount(appId, (uint)count);
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.IoError:
-                    throw new InvalidOperationException("Error from I/O");
-
-                case Interop.Badge.ErrorCode.ServiceNotReady:
-                    throw new InvalidOperationException("Service is not ready");
+                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
             }
         }
 
@@ -254,21 +175,10 @@ namespace Tizen.Applications
         /// <privilege>http://tizen.org/privilege/notification</privilege>
         public static void Update(string appId, bool isDisplay)
         {
-            Interop.Badge.ErrorCode err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
-
-            switch (err)
+            BadgeError err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new ArgumentException("Invalid parameter");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.IoError:
-                    throw new InvalidOperationException("Error from I/O");
-
-                case Interop.Badge.ErrorCode.ServiceNotReady:
-                    throw new InvalidOperationException("Service is not ready");
+                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
             }
         }
 
@@ -298,50 +208,21 @@ namespace Tizen.Applications
         {
             IList<Badge> list = new List<Badge>();
 
-            Interop.Badge.ErrorCode err = Interop.Badge.Foreach((appId, count, userData) =>
+            BadgeError err = Interop.Badge.Foreach((appId, count, userData) =>
             {
                 uint display = 0;
-                Interop.Badge.ErrorCode e = Interop.Badge.GetDisplay(appId, out display);
-                switch (e)
+                BadgeError errGetDisplay = Interop.Badge.GetDisplay(appId, out display);
+                if (errGetDisplay != BadgeError.None)
                 {
-                    case Interop.Badge.ErrorCode.InvalidParameter:
-                        throw new InvalidOperationException("Invalid parameter at unmanaged code");
-
-                    case Interop.Badge.ErrorCode.PermissionDenied:
-                        throw new UnauthorizedAccessException();
-
-                    case Interop.Badge.ErrorCode.FromDb:
-                        throw new InvalidOperationException("Error from DB");
-
-                    case Interop.Badge.ErrorCode.OutOfMemory:
-                        throw new InvalidOperationException("Out-of-memory at unmanaged code");
-
-                    case Interop.Badge.ErrorCode.NotExist:
-                        throw new InvalidOperationException("Not exist");
-
-                    case Interop.Badge.ErrorCode.ServiceNotReady:
-                        throw new InvalidOperationException("Service is not ready");
+                    throw BadgeErrorFactory.GetException(errGetDisplay, "Failed to get badges ");
                 }
 
                 list.Add(new Badge(appId, (int)count, display == 0 ? false : true));
             }, IntPtr.Zero);
 
-            switch (err)
+            if (err != BadgeError.None)
             {
-                case Interop.Badge.ErrorCode.InvalidParameter:
-                    throw new InvalidOperationException("Invalid parameter at unmanaged code");
-
-                case Interop.Badge.ErrorCode.PermissionDenied:
-                    throw new UnauthorizedAccessException();
-
-                case Interop.Badge.ErrorCode.FromDb:
-                    throw new InvalidOperationException("Error from DB");
-
-                case Interop.Badge.ErrorCode.OutOfMemory:
-                    throw new InvalidOperationException("Out-of-memory at unmanaged code");
-
-                case Interop.Badge.ErrorCode.NotExist:
-                    throw new InvalidOperationException("Not exist");
+                throw BadgeErrorFactory.GetException(err, "Failed to get badges");
             }
 
             return list;
diff --git a/Tizen.Applications.Badge/Tizen.Applications/BadgeErrorFactory.cs b/Tizen.Applications.Badge/Tizen.Applications/BadgeErrorFactory.cs
new file mode 100755 (executable)
index 0000000..9443298
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace Tizen.Applications
+{
+    using System;
+    using System.Runtime.CompilerServices;
+
+    internal enum BadgeError
+    {
+        None = Tizen.Internals.Errors.ErrorCode.None,
+        InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
+        OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
+        PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
+        IoError = Tizen.Internals.Errors.ErrorCode.IoError,
+        DbError = -0x01120000 | 0x01,
+        AlreadyExists = -0x01120000 | 0x02,
+        DBusError = -0x01120000 | 0x03,
+        DoesnotExist = -0x01120000 | 0x04,
+        ServiceError = -0x01120000 | 0x05,
+        InvalidPackage = -0x01120000 | 0x06
+    }
+
+    /// <summary>
+    /// Immutable class for getting information of the badge.
+    /// </summary>
+    internal static class BadgeErrorFactory
+    {
+        private static readonly string LogTag = "Tizen.Applications.Badge";
+
+        internal static Exception GetException(BadgeError ret, string msg, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
+        {
+            Log.Error(LogTag, memberName + " : " + lineNumber);
+
+            switch (ret)
+            {
+                case BadgeError.InvalidParameter:
+                    Log.Error(LogTag, msg);
+                    return new ArgumentException(ret + " error occurred.");
+                case BadgeError.PermissionDenied:
+                    throw new UnauthorizedAccessException("Permission denied (http://tizen.org/privilege/notification)");
+                default:
+                    Log.Error(LogTag, msg);
+                    return new InvalidOperationException(ret + " error occurred.");
+            }
+        }
+    }
+}