[USB] Update exception handling code
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / 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         IoError = Tizen.Internals.Errors.ErrorCode.IoError,
27         InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
28         PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
29         NoSuchDevice = Tizen.Internals.Errors.ErrorCode.NoSuchDevice,
30         ResourceBusy = Tizen.Internals.Errors.ErrorCode.ResourceBusy,
31         TimedOut = Tizen.Internals.Errors.ErrorCode.TimedOut,
32         BrokenPipe = Tizen.Internals.Errors.ErrorCode.BrokenPipe,
33         InterruptedSysCall = Tizen.Internals.Errors.ErrorCode.InterruptedSysCall,
34         OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
35         NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
36         Unknown = Tizen.Internals.Errors.ErrorCode.Unknown,
37
38         NotFound = -0x024B0000 | 0x01, // USB_HOST_ERROR_NOT_FOUND,
39         Overflow = -0x024B0000 | 0x02, // USB_HOST_ERROR_OVERFLOW
40         DeviceNotOpened = -0x024B0000 | 0x03, // USB_HOST_ERROR_DEVICE_NOT_OPENED
41     }
42 }
43
44 internal static class ErrorCodeExtensions
45 {
46     private const string LogTag = "USB_HOST_CSHARP";
47
48     internal static bool IsSuccess(this Interop.ErrorCode err)
49     {
50         return err == Interop.ErrorCode.None;
51     }
52
53     internal static bool IsFailed(this Interop.ErrorCode err)
54     {
55         return !err.IsSuccess();
56     }
57
58     /// <summary>
59     /// Utility method to check for error, returns false if failed and print warning messages
60     /// </summary>
61     /// <returns>true in case of no error, false otherwise</returns>
62     internal static bool WarnIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
63     {
64         if (err.IsFailed())
65         {
66             Log.Info(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
67             Console.WriteLine($"I/{LogTag}: {msg}, err: {err.ToString()}");
68             return false;
69         }
70         return true;
71     }
72
73     /// <summary>
74     /// Utility method to check for error, returns false if failed and throw exception
75     /// </summary>
76     /// <returns>true in case of no error</returns>
77     internal static bool ThrowIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
78     {
79         if (err.IsFailed())
80         {
81             Log.Error(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
82             Console.WriteLine($"E/{LogTag}: {msg}, err: {err.ToString()}");
83             throw err.GetException(msg);
84         }
85         return true;
86     }
87
88     internal static Exception GetException(this Interop.ErrorCode err, string message)
89     {
90         string errMessage = $"{message}, err: {err.ToString()}";
91         switch (err)
92         {
93             //case Interop.ErrorCode.None:
94             case Interop.ErrorCode.PermissionDenied:
95                 return new UnauthorizedAccessException(errMessage);
96             case Interop.ErrorCode.InvalidParameter:
97                 return new ArgumentException(errMessage);
98             case Interop.ErrorCode.TimedOut:
99                 return new TimeoutException(errMessage);
100             case Interop.ErrorCode.OutOfMemory:
101                 return new OutOfMemoryException(errMessage);
102             case Interop.ErrorCode.NotSupported:
103                 return new NotSupportedException(errMessage);
104
105             case Interop.ErrorCode.IoError:
106             case Interop.ErrorCode.NoSuchDevice:
107             case Interop.ErrorCode.ResourceBusy:
108             case Interop.ErrorCode.BrokenPipe:
109             case Interop.ErrorCode.InterruptedSysCall:
110             case Interop.ErrorCode.Unknown:
111
112             case Interop.ErrorCode.NotFound:
113             case Interop.ErrorCode.Overflow:
114             case Interop.ErrorCode.DeviceNotOpened:
115
116             default: return new InvalidOperationException(errMessage);
117         }
118     }
119 }