Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Content.Download / Tizen.Content.Download / DownloadErrorFactory.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.Content.Download
21 {
22     internal enum DownloadError
23     {
24         DownloadErrorCommonCode = -0x02A00000,
25         None = ErrorCode.None,
26         InvalidParameter = ErrorCode.InvalidParameter,
27         OutOfMemory = ErrorCode.OutOfMemory,
28         NetworkUnreachable = ErrorCode.NetworkUnreachable,
29         ConnectionTimedOut = ErrorCode.ConnectionTimeout,
30         NoSpace = ErrorCode.FileNoSpaceOnDevice,
31         PermissionDenied = ErrorCode.PermissionDenied,
32         NotSupported = ErrorCode.NotSupported,
33         InvalidState = DownloadErrorCommonCode | 0x21,
34         ConnectionFailed = DownloadErrorCommonCode | 0x22,
35         InvalidUrl = DownloadErrorCommonCode | 0x24,
36         InvalidDestination = DownloadErrorCommonCode | 0x25,
37         TooManyDownloads = DownloadErrorCommonCode | 0x26,
38         QueueFull = DownloadErrorCommonCode | 0x27,
39         AlreadyCompleted = DownloadErrorCommonCode | 0x28,
40         FileAlreadyExists = DownloadErrorCommonCode | 0x29,
41         CannotResume = DownloadErrorCommonCode | 0x2a,
42         FieldNotFound = DownloadErrorCommonCode | 0x2b,
43         TooManyRedirects = DownloadErrorCommonCode | 0x30,
44         UnhandledHttpCode = DownloadErrorCommonCode | 0x31,
45         RequestTimeout = DownloadErrorCommonCode | 0x32,
46         ResponseTimeout = DownloadErrorCommonCode | 0x33,
47         SystemDown = DownloadErrorCommonCode | 0x34,
48         IdNotFound = DownloadErrorCommonCode | 0x35,
49         InvalidNetworkType = DownloadErrorCommonCode | 0x36,
50         NoData = ErrorCode.NoData,
51         IoError = ErrorCode.IoError
52     }
53
54     internal static class DownloadErrorFactory
55     {
56         internal static void ThrowException(int errorCode, string errorMessage = null, string paramName = null)
57         {
58             DownloadError err = (DownloadError)errorCode;
59             if (String.IsNullOrEmpty(errorMessage))
60             {
61                 errorMessage = err.ToString();
62             }
63             switch ((DownloadError)errorCode)
64             {
65                 case DownloadError.InvalidParameter:
66                 case DownloadError.InvalidUrl:
67                 case DownloadError.InvalidDestination:
68                 case DownloadError.InvalidNetworkType: throw new ArgumentException(errorMessage, paramName);
69                 case DownloadError.OutOfMemory:
70                 case DownloadError.NetworkUnreachable:
71                 case DownloadError.ConnectionTimedOut:
72                 case DownloadError.NoSpace:
73                 case DownloadError.InvalidState:
74                 case DownloadError.ConnectionFailed:
75                 case DownloadError.TooManyDownloads:
76                 case DownloadError.QueueFull:
77                 case DownloadError.AlreadyCompleted:
78                 case DownloadError.FileAlreadyExists:
79                 case DownloadError.CannotResume:
80                 case DownloadError.FieldNotFound:
81                 case DownloadError.TooManyRedirects:
82                 case DownloadError.UnhandledHttpCode:
83                 case DownloadError.RequestTimeout:
84                 case DownloadError.ResponseTimeout:
85                 case DownloadError.SystemDown:
86                 case DownloadError.IdNotFound:
87                 case DownloadError.NoData:
88                 case DownloadError.IoError: throw new InvalidOperationException(errorMessage);
89                 case DownloadError.NotSupported: throw new NotSupportedException(errorMessage);
90                 case DownloadError.PermissionDenied: throw new UnauthorizedAccessException(errorMessage);
91             }
92         }
93     }
94 }