Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.OAuth2 / Tizen.Account.OAuth2 / OAuth2ErrorFactory.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 Tizen.Internals.Errors;
19
20 namespace Tizen.Account.OAuth2
21 {
22     internal enum OAuth2Error
23     {
24         // Tizen Account Oauth Error
25         // TIZEN_ERROR_ACCOUNT_OAUTH       -0x01010000
26
27         None = ErrorCode.None,
28         OutOfMemory = ErrorCode.OutOfMemory,
29         InvalidParameter = ErrorCode.InvalidParameter,
30         AlreadyInProgress = ErrorCode.AlreadyInProgress,
31         NotSupported = ErrorCode.NotSupported,
32         PermissionDenied = ErrorCode.PermissionDenied,
33         ParseFailed = -0x01010000 | 0X01,
34         NetworkError = -0x01010000 | 0X02,
35         Server = -0x01010000 | 0X03,
36         UserCancelled = -0x01010000 | 0X04,
37         ValueNotFound = -0x01010000 | 0X05,
38         Unknown = ErrorCode.Unknown
39     }
40
41     internal class ErrorFactory
42     {
43         internal static string LogTag = "Tizen.Account.OAuth2";
44
45         internal static Exception GetException(IntPtr error)
46         {
47             int serverErrorCode, platformErrorCode;
48             int ret = Interop.Error.GetCode(error, out serverErrorCode, out platformErrorCode);
49             if (ret != (int)OAuth2Error.None)
50             {
51                 Log.Debug(ErrorFactory.LogTag, "error code can't be found");
52             }
53
54             string errorDescription;
55             ret = Interop.Error.GetDescription(error, out errorDescription);
56             if (ret != (int)OAuth2Error.None)
57             {
58                 Log.Debug(ErrorFactory.LogTag, "error description can't be found");
59             }
60
61             string uri;
62             Interop.Error.GetUri(error, out uri);
63             if (ret != (int)OAuth2Error.None)
64             {
65                 Log.Debug(ErrorFactory.LogTag, "error uri can't be found");
66             }
67
68             var errorResponse = new OAuth2ErrorResponse()
69             {
70                 PlatformErrorCode = platformErrorCode,
71                 ServerErrorCode = serverErrorCode,
72                 ErrorUri = uri,
73                 Error = errorDescription
74             };
75
76             return new OAuth2Exception() { Error = errorResponse };
77         }
78
79         internal static Exception GetException(int error)
80         {
81             if ((OAuth2Error)error == OAuth2Error.OutOfMemory)
82             {
83                 return new OutOfMemoryException("Out of memory");
84             }
85             else if ((OAuth2Error)error == OAuth2Error.InvalidParameter)
86             {
87                 return new ArgumentException("Invalid parameter");
88             }
89             else if ((OAuth2Error)error == OAuth2Error.AlreadyInProgress)
90             {
91                 return new InvalidOperationException("Request already in progress");
92             }
93             else if ((OAuth2Error)error == OAuth2Error.NotSupported)
94             {
95                 return new NotSupportedException("Not supported");
96             }
97             else if ((OAuth2Error)error == OAuth2Error.PermissionDenied)
98             {
99                 return new UnauthorizedAccessException("Permission denied");
100             }
101             else if ((OAuth2Error)error == OAuth2Error.ParseFailed)
102             {
103                 return new FormatException("Parsing failed");
104             }
105             else if ((OAuth2Error)error == OAuth2Error.NetworkError)
106             {
107                 return new Exception("Networking error occured");
108             }
109             else if ((OAuth2Error)error == OAuth2Error.Server)
110             {
111                 return new Exception("Server error");
112             }
113             else if ((OAuth2Error)error == OAuth2Error.UserCancelled)
114             {
115                 return new Exception("User cancelled");
116             }
117             else if ((OAuth2Error)error == OAuth2Error.ValueNotFound)
118             {
119                 return new ArgumentException("Value not found");
120             }
121             else
122             {
123                 return new Exception("Unknown error");
124             }
125         }
126     }
127 }