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 TwitterError ErrorResponse { get; }
39 public TwitterApiException()
43 public TwitterApiException(string message)
48 public TwitterApiException(string message, Exception innerException)
49 : base(message, innerException)
53 public TwitterApiException(HttpStatusCode statusCode, string responseText)
54 : base(statusCode.ToString(), responseText)
58 public TwitterApiException(TwitterError error, string responseText)
59 : base(FormatTwitterError(error), responseText)
61 this.ErrorResponse = error;
64 protected TwitterApiException(SerializationInfo info, StreamingContext context)
67 this.ErrorResponse = (TwitterError)info.GetValue("ErrorResponse", typeof(TwitterError));
70 private TwitterApiException(string message, string responseText, Exception innerException)
71 : base(message, responseText, innerException)
75 public override void GetObjectData(SerializationInfo info, StreamingContext context)
77 base.GetObjectData(info, context);
79 info.AddValue("ErrorResponse", this.ErrorResponse);
82 public static TwitterApiException CreateFromException(HttpRequestException ex)
83 => new TwitterApiException(ex.InnerException?.Message ?? ex.Message, ex);
85 public static TwitterApiException CreateFromException(OperationCanceledException ex)
86 => new TwitterApiException("Timeout", ex);
88 public static TwitterApiException CreateFromException(SerializationException ex, string responseText)
89 => new TwitterApiException("Invalid JSON", responseText, ex);
91 private static string FormatTwitterError(TwitterError error)
92 => string.Join(",", error.Errors.Select(x => x.ToString()));