Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.DataControl / Tizen.Applications.DataControl / ErrorFactory.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.CompilerServices;
19
20 namespace Tizen.Applications.DataControl
21 {
22     internal static class ErrorFactory
23     {
24         private const string LogTag = "Tizen.Applications.DataControl";
25
26         internal static void ThrowException(ResultType errorCode, bool ignoreType, string errorMessage = null,
27             [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
28         {
29             Log.Error(LogTag, $"{memberName}({lineNumber.ToString()}) : {filePath}");
30             if (ignoreType)
31             {
32                 throw new InvalidOperationException(string.IsNullOrEmpty(errorMessage) ? "error code : " + errorCode.ToString() :
33                      $"{errorMessage} - {errorCode}");
34             }
35
36             switch (errorCode)
37             {
38                 case ResultType.Success:
39                     return;
40                 case ResultType.OutOfMemory:
41                 case ResultType.IoError:
42                     throw new InvalidOperationException(string.IsNullOrEmpty(errorMessage) ? "error code : " + errorCode.ToString() :
43                         $"{errorMessage} - {errorCode}");
44                 case ResultType.InvalidParameter:
45                     Log.Error(LogTag, "Invalid parameter : " + errorMessage);
46                     throw new ArgumentException(string.IsNullOrEmpty(errorMessage) ? "Invalid parameter" : "Invalid parameter : " + errorMessage);
47                 case ResultType.PermissionDenied:
48                     Log.Error(LogTag, "Permission denied : " + errorMessage);
49                     throw new UnauthorizedAccessException(string.IsNullOrEmpty(errorMessage) ? "Permission denied" : "Permission denied : " + errorMessage);
50                 case ResultType.MaxExceed:
51                     Log.Error(LogTag, "Too long argument : " + errorMessage);
52                     throw new ArgumentOutOfRangeException(string.IsNullOrEmpty(errorMessage) ? "Too long argument" : "Too long argument : " + errorMessage);                
53                 default:
54                     Log.Error(LogTag, $"Unknown error : {errorMessage} - {errorCode}");
55                     throw new InvalidOperationException(string.IsNullOrEmpty(errorMessage) ? "Unknown error : " + errorCode.ToString() :
56                         $"Unknown error : {errorMessage} - {errorCode}");
57             }
58         }
59     }
60 }