OSDN Git Service

bit.ly API を使用する処理をBitlyApiクラスに分離
[opentween/open-tween.git] / OpenTween / Api / TwitterApiException.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Net;
26 using System.Net.Http;
27 using System.Runtime.Serialization;
28 using System.Text;
29 using System.Threading.Tasks;
30 using OpenTween.Api.DataModel;
31
32 namespace OpenTween.Api
33 {
34     [Serializable]
35     public class TwitterApiException : WebApiException
36     {
37         public TwitterError ErrorResponse { get; }
38
39         public TwitterApiException()
40         {
41         }
42
43         public TwitterApiException(string message)
44             : base(message)
45         {
46         }
47
48         public TwitterApiException(string message, Exception innerException)
49             : base(message, innerException)
50         {
51         }
52
53         public TwitterApiException(HttpStatusCode statusCode, string responseText)
54             : base(statusCode.ToString(), responseText)
55         {
56         }
57
58         public TwitterApiException(TwitterError error, string responseText)
59             : base(FormatTwitterError(error), responseText)
60         {
61             this.ErrorResponse = error;
62         }
63
64         protected TwitterApiException(SerializationInfo info, StreamingContext context)
65             : base(info, context)
66         {
67             this.ErrorResponse = (TwitterError)info.GetValue("ErrorResponse", typeof(TwitterError));
68         }
69
70         private TwitterApiException(string message, string responseText, Exception innerException)
71             : base(message, responseText, innerException)
72         {
73         }
74
75         public override void GetObjectData(SerializationInfo info, StreamingContext context)
76         {
77             base.GetObjectData(info, context);
78
79             info.AddValue("ErrorResponse", this.ErrorResponse);
80         }
81
82         public static TwitterApiException CreateFromException(HttpRequestException ex)
83             => new TwitterApiException(ex.InnerException?.Message ?? ex.Message, ex);
84
85         public static TwitterApiException CreateFromException(OperationCanceledException ex)
86             => new TwitterApiException("Timeout", ex);
87
88         public static TwitterApiException CreateFromException(SerializationException ex, string responseText)
89             => new TwitterApiException("Invalid JSON", responseText, ex);
90
91         private static string FormatTwitterError(TwitterError error)
92             => string.Join(",", error.Errors.Select(x => x.ToString()));
93     }
94 }