1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
26 using System.Net.Http;
27 using System.Runtime.Serialization;
29 using System.Threading.Tasks;
30 using OpenTween.Api.DataModel;
32 namespace OpenTween.Api
35 public class TwitterApiException : WebApiException
37 public HttpStatusCode StatusCode { get; }
38 public TwitterError ErrorResponse { get; }
40 public TwitterApiException()
44 public TwitterApiException(string message)
49 public TwitterApiException(string message, Exception innerException)
50 : base(message, innerException)
54 public TwitterApiException(HttpStatusCode statusCode, string responseText)
55 : base(statusCode.ToString(), responseText)
57 this.StatusCode = statusCode;
60 public TwitterApiException(HttpStatusCode statusCode, TwitterError error, string responseText)
61 : base(FormatTwitterError(error), responseText)
63 this.StatusCode = statusCode;
64 this.ErrorResponse = error;
67 protected TwitterApiException(SerializationInfo info, StreamingContext context)
70 this.StatusCode = (HttpStatusCode)info.GetValue("StatusCode", typeof(HttpStatusCode));
71 this.ErrorResponse = (TwitterError)info.GetValue("ErrorResponse", typeof(TwitterError));
74 private TwitterApiException(string message, string responseText, Exception innerException)
75 : base(message, responseText, innerException)
79 public override void GetObjectData(SerializationInfo info, StreamingContext context)
81 base.GetObjectData(info, context);
83 info.AddValue("StatusCode", this.StatusCode);
84 info.AddValue("ErrorResponse", this.ErrorResponse);
87 public static TwitterApiException CreateFromException(HttpRequestException ex)
88 => new TwitterApiException(ex.InnerException?.Message ?? ex.Message, ex);
90 public static TwitterApiException CreateFromException(OperationCanceledException ex)
91 => new TwitterApiException("Timeout", ex);
93 public static TwitterApiException CreateFromException(SerializationException ex, string responseText)
94 => new TwitterApiException("Invalid JSON", responseText, ex);
96 private static string FormatTwitterError(TwitterError error)
97 => string.Join(",", error.Errors.Select(x => x.ToString()));