Tizen.Maps Setup
[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         ServiceNotAvailable = -0x02C20000 | 0x01, // MAPS_ERROR_SERVICE_NOT_AVAILABLE
38         NotFound = -0x02C20000 | 0x02, // MAPS_ERROR_NOT_FOUND
39     }
40 }
41
42 internal static class ErrorCodeExtensions
43 {
44     private const string LogTag = "Tizen.Maps";
45
46     internal static bool IsSuccess(this Interop.ErrorCode err)
47     {
48         return err == Interop.ErrorCode.None;
49     }
50
51     internal static bool IsFailed(this Interop.ErrorCode err)
52     {
53         return !err.IsSuccess();
54     }
55
56     /// <summary>
57     /// Utility method to check for error, returns false if failed and print warning messages
58     /// </summary>
59     /// <returns>true in case of no error, false otherwise</returns>
60     internal static bool WarnIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
61     {
62         if (err.IsFailed())
63         {
64             Log.Debug(LogTag, string.Format("{0}, err: {1}", msg, err.ToString()), file, func, line);
65             return false;
66         }
67         return true;
68     }
69
70     /// <summary>
71     /// Utility method to check for error, returns false if failed and throw exception
72     /// </summary>
73     /// <returns>true in case of no error</returns>
74     internal static bool ThrowIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
75     {
76         if (err.IsFailed())
77         {
78             Log.Error(LogTag, string.Format("{0}, err: {1}", msg, err.ToString()), file, func, line);
79             throw err.GetException(msg);
80         }
81         return true;
82     }
83
84     internal static Exception GetException(this Interop.ErrorCode err, string message)
85     {
86         string errMessage = string.Format("{0}, err: {1}", message, err.ToString());
87         switch (err)
88         {
89             //case ErrorCode.None:
90             case Interop.ErrorCode.PermissionDenied: return new UnauthorizedAccessException(errMessage);
91             case Interop.ErrorCode.InvalidParameter: return new ArgumentException(errMessage);
92             case Interop.ErrorCode.OutOfMemory:
93             case Interop.ErrorCode.NotSupported:
94             case Interop.ErrorCode.ConnectionTimeOut:
95             case Interop.ErrorCode.NetworkUnreachable:
96             case Interop.ErrorCode.InvalidOperation:
97             case Interop.ErrorCode.KeyNotAvailable:
98             case Interop.ErrorCode.ResourceBusy:
99             case Interop.ErrorCode.Canceled:
100             case Interop.ErrorCode.Unknown:
101             case Interop.ErrorCode.ServiceNotAvailable:
102             case Interop.ErrorCode.NotFound:
103             default: return new InvalidOperationException(errMessage);
104         }
105     }
106 }