Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Interop / Interop.ErrorCode.cs
1 /*
2  * Copyright (c) 2016 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 using Tizen;
20
21 internal static partial class Interop
22 {
23     internal enum ErrorCode
24     {
25         None = Tizen.Internals.Errors.ErrorCode.None,
26         PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
27         OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
28         InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
29         NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
30         ConnectionTimeOut = Tizen.Internals.Errors.ErrorCode.ConnectionTimeout,
31         NetworkUnreachable = Tizen.Internals.Errors.ErrorCode.NetworkUnreachable,
32         InvalidOperation = Tizen.Internals.Errors.ErrorCode.InvalidOperation,
33         KeyNotAvailable = Tizen.Internals.Errors.ErrorCode.KeyNotAvailable,
34         ResourceBusy = Tizen.Internals.Errors.ErrorCode.ResourceBusy,
35         Canceled = Tizen.Internals.Errors.ErrorCode.Canceled,
36         Unknown = Tizen.Internals.Errors.ErrorCode.Unknown,
37         UserNotConsented = Tizen.Internals.Errors.ErrorCode.UserNotConsented,
38         ServiceNotAvailable = -0x02C20000 | 0x01, // MAPS_ERROR_SERVICE_NOT_AVAILABLE
39         NotFound = -0x02C20000 | 0x02, // MAPS_ERROR_NOT_FOUND
40     }
41 }
42
43 internal static class ErrorCodeExtensions
44 {
45     private const string LogTag = "Tizen.Maps";
46
47     internal static bool IsSuccess(this Interop.ErrorCode err)
48     {
49         return err == Interop.ErrorCode.None;
50     }
51
52     internal static bool IsFailed(this Interop.ErrorCode err)
53     {
54         return !err.IsSuccess();
55     }
56
57     /// <summary>
58     /// The utility method to check for an error. Returns false on failure and prints warning messages.
59     /// </summary>
60     /// <returns>Returns true in case of no error, otherwise false.</returns>
61     internal static bool WarnIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
62     {
63         if (err.IsFailed())
64         {
65             Log.Debug(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
66             return false;
67         }
68         return true;
69     }
70
71     /// <summary>
72     /// The utility method to check for an error. Returns false on failure and throws an exception.
73     /// </summary>
74     /// <returns>Returns true in case of no error, otherwise false.</returns>
75     internal static bool ThrowIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
76     {
77         if (err.IsFailed())
78         {
79             Log.Error(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
80             throw err.GetException(msg);
81         }
82         return true;
83     }
84
85     internal static Exception GetException(this Interop.ErrorCode err, string message)
86     {
87         string errMessage = $"{message}, err: {err.ToString()}";
88         switch (err)
89         {
90             //case ErrorCode.None:
91             case Interop.ErrorCode.PermissionDenied: return new System.UnauthorizedAccessException(errMessage);
92             case Interop.ErrorCode.InvalidParameter: return new System.ArgumentException(errMessage);
93             case Interop.ErrorCode.OutOfMemory:
94             case Interop.ErrorCode.NotSupported:
95             case Interop.ErrorCode.ConnectionTimeOut:
96             case Interop.ErrorCode.NetworkUnreachable:
97             case Interop.ErrorCode.InvalidOperation:
98             case Interop.ErrorCode.KeyNotAvailable:
99             case Interop.ErrorCode.ResourceBusy:
100             case Interop.ErrorCode.Canceled:
101             case Interop.ErrorCode.Unknown:
102             case Interop.ErrorCode.ServiceNotAvailable:
103             case Interop.ErrorCode.NotFound:
104             default: return new System.InvalidOperationException(errMessage);
105         }
106     }
107 }