• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

開発に使用するリポジトリ


Commit MetaInfo

Revisiónbf2677bd6dd0761de4034589419cb6abf4043570 (tree)
Tiempo2013-08-25 15:08:07
AutorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

REST APIのエラー処理を統一

https://dev.twitter.com/docs/error-codes-responses に記載されているエラーコードを考慮

Cambiar Resumen

Diferencia incremental

--- a/OpenTween/DataModel.cs
+++ b/OpenTween/DataModel.cs
@@ -5,6 +5,7 @@
55 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
66 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
77 // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8+// (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
89 // All rights reserved.
910 //
1011 // This file is part of OpenTween.
@@ -428,8 +429,80 @@ namespace OpenTween
428429 [DataContract]
429430 public class ErrorResponse
430431 {
431- [DataMember(Name = "request")] public string Request;
432- [DataMember(Name = "error")] public string ErrMsg;
432+ [DataMember(Name = "errors")] public ErrorItem[] Errors;
433+ }
434+
435+ [DataContract]
436+ public class ErrorItem
437+ {
438+ [DataMember(Name = "code")] public ErrorCode Code;
439+ [DataMember(Name = "message")] public string Message;
440+
441+ public override string ToString()
442+ {
443+ if (Enum.IsDefined(typeof(ErrorCode), this.Code))
444+ return this.Code.ToString();
445+ else
446+ return this.Message;
447+ }
448+ }
449+
450+ /// <summary>
451+ /// Twitter API から返されるエラーコード
452+ /// </summary>
453+ /// <remarks>
454+ /// https://dev.twitter.com/docs/error-codes-responses を参照
455+ /// </remarks>
456+ public enum ErrorCode : int
457+ {
458+ /// <summary>
459+ /// 不正なリクエスト等によって認証を完了できない場合に発生する。大体クライアントのせい
460+ /// </summary>
461+ AuthError = 32,
462+ /// <summary>
463+ /// 指定されたリソースが存在しません。HTTP 404 と同等
464+ /// </summary>
465+ NotFound = 34,
466+ /// <summary>
467+ /// REST API v1 は星になりました
468+ /// </summary>
469+ APIv1Retired = 68,
470+ /// <summary>
471+ /// レートリミットに到達しました
472+ /// </summary>
473+ RateLimit = 88,
474+ /// <summary>
475+ /// アクセストークンが無効です。不正なトークンまたはユーザーによって失効されています
476+ /// </summary>
477+ InvalidToken = 89,
478+ /// <summary>
479+ /// アカウントが凍結されています
480+ /// </summary>
481+ SuspendedAccount = 64,
482+ /// <summary>
483+ /// サーバーの過負荷によって一時的にアクセスできません
484+ /// </summary>
485+ OverCapacity = 130,
486+ /// <summary>
487+ /// サーバーの内部エラー
488+ /// </summary>
489+ InternalError = 131,
490+ /// <summary>
491+ /// ユーザーからブロックされている (公式ドキュメントに記述無し)
492+ /// </summary>
493+ Blocked = 136,
494+ /// <summary>
495+ /// oauth_timestamp の時刻が無効。クライアントかサーバーの時計が大幅にずれている
496+ /// </summary>
497+ TimestampOutOfRange = 135,
498+ /// <summary>
499+ /// 投稿されたステータスが重複しています
500+ /// </summary>
501+ DuplicateStatus = 187,
502+ /// <summary>
503+ /// 認証が必要な API で認証データが含まれていない、または認証データが不正
504+ /// </summary>
505+ AuthenticationRequired = 215,
433506 }
434507
435508 [DataContract]
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -1,7 +1,9 @@
11 更新履歴
22
33 ==== Ver 1.1.3-beta1(2013/xx/xx)
4+ * CHG: Twitter API のエラー処理を改善
45 * FIX: 「片思いユーザーリストを取得する」を無効にするとRT非表示設定が反映されない問題の修正 (thx @ui_nyan!)
6+ * FIX: ブロックされているユーザーのツイートをふぁぼった時にUserStreamsが切断されてしまう問題の修正
57
68 ==== Ver 1.1.2(2013/07/06)
79 * NEW: 画像アップロード対応サービスについっぷるフォトを追加
--- a/OpenTween/Tween.cs
+++ b/OpenTween/Tween.cs
@@ -2575,19 +2575,7 @@ namespace OpenTween
25752575 bw.ReportProgress(200);
25762576 if (string.IsNullOrEmpty(args.status.imagePath))
25772577 {
2578- for (int i = 0; i <= 1; i++)
2579- {
2580- ret = tw.PostStatus(args.status.status, args.status.inReplyToId);
2581- if (string.IsNullOrEmpty(ret) ||
2582- ret.StartsWith("OK:") ||
2583- ret.StartsWith("Outputz:") ||
2584- ret.StartsWith("Warn:") ||
2585- ret == "Err:Status is a duplicate." ||
2586- args.status.status.StartsWith("D", StringComparison.OrdinalIgnoreCase) ||
2587- args.status.status.StartsWith("DM", StringComparison.OrdinalIgnoreCase) ||
2588- Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid)
2589- break;
2590- }
2578+ ret = tw.PostStatus(args.status.status, args.status.inReplyToId);
25912579 }
25922580 else
25932581 {
--- a/OpenTween/Twitter.cs
+++ b/OpenTween/Twitter.cs
@@ -5,6 +5,7 @@
55 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
66 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
77 // (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8+// (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
89 // All rights reserved.
910 //
1011 // This file is part of OpenTween.
@@ -28,6 +29,7 @@ using System.Diagnostics;
2829 using System.IO;
2930 using System.Linq;
3031 using System.Net;
32+using System.Runtime.CompilerServices;
3133 using System.Runtime.Serialization;
3234 using System.Runtime.Serialization.Json;
3335 using System.Text;
@@ -191,10 +193,10 @@ namespace OpenTween
191193
192194 public string Authenticate(string username, string password)
193195 {
196+ this.ResetApiStatus();
197+
194198 HttpStatusCode res;
195199 var content = "";
196-
197- this.ResetApiStatus();
198200 try
199201 {
200202 res = twCon.AuthUserAndPass(username, password, ref content);
@@ -204,41 +206,12 @@ namespace OpenTween
204206 return "Err:" + ex.Message;
205207 }
206208
207- switch (res)
208- {
209- case HttpStatusCode.OK:
210- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
211- _uname = username.ToLower();
212- if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream();
213- return "";
214- case HttpStatusCode.Unauthorized:
215- {
216- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
217- var errMsg = GetErrorMessageJson(content);
218- if (string.IsNullOrEmpty(errMsg))
219- {
220- return Properties.Resources.Unauthorized + Environment.NewLine + content;
221- }
222- else
223- {
224- return "Auth error:" + errMsg;
225- }
226- }
227- case HttpStatusCode.Forbidden:
228- {
229- var errMsg = GetErrorMessageJson(content);
230- if (string.IsNullOrEmpty(errMsg))
231- {
232- return "Err:Forbidden";
233- }
234- else
235- {
236- return "Err:" + errMsg;
237- }
238- }
239- default:
240- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
241- }
209+ var err = this.CheckStatusCode(res, content);
210+ if (err != null) return err;
211+
212+ _uname = username.ToLower();
213+ if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream();
214+ return "";
242215 }
243216
244217 public string StartAuthentication(ref string pinPageUrl)
@@ -261,10 +234,9 @@ namespace OpenTween
261234
262235 public string Authenticate(string pinCode)
263236 {
264- HttpStatusCode res;
265- var content = "";
266-
267237 this.ResetApiStatus();
238+
239+ HttpStatusCode res;
268240 try
269241 {
270242 res = twCon.AuthGetAccessToken(pinCode);
@@ -274,41 +246,12 @@ namespace OpenTween
274246 return "Err:" + "Failed to access auth acc server.";
275247 }
276248
277- switch (res)
278- {
279- case HttpStatusCode.OK:
280- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
281- _uname = Username.ToLower();
282- if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream();
283- return "";
284- case HttpStatusCode.Unauthorized:
285- {
286- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
287- var errMsg = GetErrorMessageJson(content);
288- if (string.IsNullOrEmpty(errMsg))
289- {
290- return "Check the PIN or retry." + Environment.NewLine + content;
291- }
292- else
293- {
294- return "Auth error:" + errMsg;
295- }
296- }
297- case HttpStatusCode.Forbidden:
298- {
299- var errMsg = GetErrorMessageJson(content);
300- if (string.IsNullOrEmpty(errMsg))
301- {
302- return "Err:Forbidden";
303- }
304- else
305- {
306- return "Err:" + errMsg;
307- }
308- }
309- default:
310- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
311- }
249+ var err = this.CheckStatusCode(res, null);
250+ if (err != null) return err;
251+
252+ _uname = Username.ToLower();
253+ if (AppendSettingDialog.Instance.UserstreamStartup) this.ReconnectUserStream();
254+ return "";
312255 }
313256
314257 public void ClearAuthInfo()
@@ -320,9 +263,8 @@ namespace OpenTween
320263
321264 public void VerifyCredentials()
322265 {
323- HttpStatusCode res = HttpStatusCode.BadRequest;
266+ HttpStatusCode res;
324267 var content = "";
325-
326268 try
327269 {
328270 res = twCon.VerifyCredentials(ref content);
@@ -348,36 +290,6 @@ namespace OpenTween
348290 }
349291 }
350292
351- private string GetErrorMessageJson(string content)
352- {
353- try
354- {
355- if (!string.IsNullOrEmpty(content))
356- {
357- using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(content), XmlDictionaryReaderQuotas.Max))
358- {
359- var xElm = XElement.Load(jsonReader);
360- if (xElm.Element("error") != null)
361- {
362- return xElm.Element("error").Value;
363- }
364- else
365- {
366- return "";
367- }
368- }
369- }
370- else
371- {
372- return "";
373- }
374- }
375- catch(Exception)
376- {
377- return "";
378- }
379- }
380-
381293 public void Initialize(string token, string tokenSecret, string username, long userId)
382294 {
383295 //OAuth認証
@@ -556,7 +468,6 @@ namespace OpenTween
556468
557469 public string PostStatus(string postStr, long? reply_to)
558470 {
559-
560471 if (MyCommon._endingFlag) return "";
561472
562473 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
@@ -568,7 +479,7 @@ namespace OpenTween
568479 return SendDirectMessage(postStr);
569480 }
570481
571- HttpStatusCode res = HttpStatusCode.BadRequest;
482+ HttpStatusCode res;
572483 var content = "";
573484 try
574485 {
@@ -579,87 +490,44 @@ namespace OpenTween
579490 return "Err:" + ex.Message;
580491 }
581492
582- switch (res)
493+ // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
494+ if (res == HttpStatusCode.NotFound) return "";
495+
496+ var err = this.CheckStatusCode(res, content);
497+ if (err != null) return err;
498+
499+ TwitterDataModel.Status status;
500+ try
583501 {
584- case HttpStatusCode.OK:
585- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
586- TwitterDataModel.Status status;
587- try
588- {
589- status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
590- }
591- catch(SerializationException ex)
592- {
593- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
594- return "Err:Json Parse Error(DataContractJsonSerializer)";
595- }
596- catch(Exception ex)
597- {
598- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
599- return "Err:Invalid Json!";
600- }
601- _followersCount = status.User.FollowersCount;
602- _friendsCount = status.User.FriendsCount;
603- _statusesCount = status.User.StatusesCount;
604- _location = status.User.Location;
605- _bio = status.User.Description;
502+ status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
503+ }
504+ catch(SerializationException ex)
505+ {
506+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
507+ return "Err:Json Parse Error(DataContractJsonSerializer)";
508+ }
509+ catch(Exception ex)
510+ {
511+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
512+ return "Err:Invalid Json!";
513+ }
514+ _followersCount = status.User.FollowersCount;
515+ _friendsCount = status.User.FriendsCount;
516+ _statusesCount = status.User.StatusesCount;
517+ _location = status.User.Location;
518+ _bio = status.User.Description;
606519
607- if (IsPostRestricted(status))
608- {
609- return "OK:Delaying?";
610- }
611- if (op.Post(postStr.Length))
612- {
613- return "";
614- }
615- else
616- {
617- return "Outputz:Failed";
618- }
619- case HttpStatusCode.NotFound:
520+ if (IsPostRestricted(status))
521+ {
522+ return "OK:Delaying?";
523+ }
524+ if (op.Post(postStr.Length))
525+ {
620526 return "";
621- case HttpStatusCode.Forbidden:
622- case HttpStatusCode.BadRequest:
623- {
624- var errMsg = GetErrorMessageJson(content);
625- if (string.IsNullOrEmpty(errMsg))
626- {
627- return "Warn:" + res.ToString();
628- }
629- else
630- {
631- return "Warn:" + errMsg;
632- }
633- }
634- case HttpStatusCode.Conflict:
635- case HttpStatusCode.ExpectationFailed:
636- case HttpStatusCode.Gone:
637- case HttpStatusCode.LengthRequired:
638- case HttpStatusCode.MethodNotAllowed:
639- case HttpStatusCode.NotAcceptable:
640- case HttpStatusCode.PaymentRequired:
641- case HttpStatusCode.PreconditionFailed:
642- case HttpStatusCode.RequestedRangeNotSatisfiable:
643- case HttpStatusCode.RequestEntityTooLarge:
644- case HttpStatusCode.RequestTimeout:
645- case HttpStatusCode.RequestUriTooLong:
646- //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない
647- return "Warn:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
648- case HttpStatusCode.Unauthorized:
649- {
650- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
651- var errMsg = GetErrorMessageJson(content);
652- if (string.IsNullOrEmpty(errMsg))
653- {
654- return Properties.Resources.Unauthorized;
655- }
656- else
657- {
658- return "Auth err:" + errMsg;
659- }
660- }
661- default:
662- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
527+ }
528+ else
529+ {
530+ return "Outputz:Failed";
663531 }
664532 }
665533
@@ -671,7 +539,7 @@ namespace OpenTween
671539
672540 postStr = postStr.Trim();
673541
674- HttpStatusCode res = HttpStatusCode.BadRequest;
542+ HttpStatusCode res;
675543 var content = "";
676544 try
677545 {
@@ -682,87 +550,44 @@ namespace OpenTween
682550 return "Err:" + ex.Message;
683551 }
684552
685- switch (res)
553+ // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
554+ if (res == HttpStatusCode.NotFound) return "";
555+
556+ var err = this.CheckStatusCode(res, content);
557+ if (err != null) return err;
558+
559+ TwitterDataModel.Status status;
560+ try
686561 {
687- case HttpStatusCode.OK:
688- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
689- TwitterDataModel.Status status;
690- try
691- {
692- status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
693- }
694- catch(SerializationException ex)
695- {
696- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
697- return "Err:Json Parse Error(DataContractJsonSerializer)";
698- }
699- catch(Exception ex)
700- {
701- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
702- return "Err:Invalid Json!";
703- }
704- _followersCount = status.User.FollowersCount;
705- _friendsCount = status.User.FriendsCount;
706- _statusesCount = status.User.StatusesCount;
707- _location = status.User.Location;
708- _bio = status.User.Description;
562+ status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
563+ }
564+ catch(SerializationException ex)
565+ {
566+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
567+ return "Err:Json Parse Error(DataContractJsonSerializer)";
568+ }
569+ catch(Exception ex)
570+ {
571+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
572+ return "Err:Invalid Json!";
573+ }
574+ _followersCount = status.User.FollowersCount;
575+ _friendsCount = status.User.FriendsCount;
576+ _statusesCount = status.User.StatusesCount;
577+ _location = status.User.Location;
578+ _bio = status.User.Description;
709579
710- if (IsPostRestricted(status))
711- {
712- return "OK:Delaying?";
713- }
714- if (op.Post(postStr.Length))
715- {
716- return "";
717- }
718- else
719- {
720- return "Outputz:Failed";
721- }
722- case HttpStatusCode.NotFound:
580+ if (IsPostRestricted(status))
581+ {
582+ return "OK:Delaying?";
583+ }
584+ if (op.Post(postStr.Length))
585+ {
723586 return "";
724- case HttpStatusCode.Forbidden:
725- case HttpStatusCode.BadRequest:
726- {
727- var errMsg = GetErrorMessageJson(content);
728- if (string.IsNullOrEmpty(errMsg))
729- {
730- return "Warn:" + res.ToString();
731- }
732- else
733- {
734- return "Warn:" + errMsg;
735- }
736- }
737- case HttpStatusCode.Conflict:
738- case HttpStatusCode.ExpectationFailed:
739- case HttpStatusCode.Gone:
740- case HttpStatusCode.LengthRequired:
741- case HttpStatusCode.MethodNotAllowed:
742- case HttpStatusCode.NotAcceptable:
743- case HttpStatusCode.PaymentRequired:
744- case HttpStatusCode.PreconditionFailed:
745- case HttpStatusCode.RequestedRangeNotSatisfiable:
746- case HttpStatusCode.RequestEntityTooLarge:
747- case HttpStatusCode.RequestTimeout:
748- case HttpStatusCode.RequestUriTooLong:
749- //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない
750- return "Warn:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
751- case HttpStatusCode.Unauthorized:
752- {
753- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
754- var errMsg = GetErrorMessageJson(content);
755- if (string.IsNullOrEmpty(errMsg))
756- {
757- return Properties.Resources.Unauthorized;
758- }
759- else
760- {
761- return "Auth err:" + errMsg;
762- }
763- }
764- default:
765- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
587+ }
588+ else
589+ {
590+ return "Outputz:Failed";
766591 }
767592 }
768593
@@ -779,11 +604,10 @@ namespace OpenTween
779604
780605 postStr = postStr.Trim();
781606
782- HttpStatusCode res = HttpStatusCode.BadRequest;
783- var content = "";
784-
785607 var mc = Regex.Match(postStr, "^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
786608
609+ HttpStatusCode res;
610+ var content = "";
787611 try
788612 {
789613 res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content);
@@ -793,82 +617,37 @@ namespace OpenTween
793617 return "Err:" + ex.Message;
794618 }
795619
796- switch (res)
620+ var err = this.CheckStatusCode(res, content);
621+ if (err != null) return err;
622+
623+ TwitterDataModel.Directmessage status;
624+ try
797625 {
798- case HttpStatusCode.OK:
799- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
800- TwitterDataModel.Directmessage status;
801- try
802- {
803- status = MyCommon.CreateDataFromJson<TwitterDataModel.Directmessage>(content);
804- }
805- catch(SerializationException ex)
806- {
807- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
808- return "Err:Json Parse Error(DataContractJsonSerializer)";
809- }
810- catch(Exception ex)
811- {
812- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
813- return "Err:Invalid Json!";
814- }
815- _followersCount = status.Sender.FollowersCount;
816- _friendsCount = status.Sender.FriendsCount;
817- _statusesCount = status.Sender.StatusesCount;
818- _location = status.Sender.Location;
819- _bio = status.Sender.Description;
626+ status = MyCommon.CreateDataFromJson<TwitterDataModel.Directmessage>(content);
627+ }
628+ catch(SerializationException ex)
629+ {
630+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
631+ return "Err:Json Parse Error(DataContractJsonSerializer)";
632+ }
633+ catch(Exception ex)
634+ {
635+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
636+ return "Err:Invalid Json!";
637+ }
638+ _followersCount = status.Sender.FollowersCount;
639+ _friendsCount = status.Sender.FriendsCount;
640+ _statusesCount = status.Sender.StatusesCount;
641+ _location = status.Sender.Location;
642+ _bio = status.Sender.Description;
820643
821- if (op.Post(postStr.Length))
822- {
823- return "";
824- }
825- else
826- {
827- return "Outputz:Failed";
828- }
829- case HttpStatusCode.Forbidden:
830- case HttpStatusCode.BadRequest:
831- {
832- var errMsg = GetErrorMessageJson(content);
833- if (string.IsNullOrEmpty(errMsg))
834- {
835- return "Warn:" + res.ToString();
836- }
837- else
838- {
839- return "Warn:" + errMsg;
840- }
841- }
842- case HttpStatusCode.Conflict:
843- case HttpStatusCode.ExpectationFailed:
844- case HttpStatusCode.Gone:
845- case HttpStatusCode.LengthRequired:
846- case HttpStatusCode.MethodNotAllowed:
847- case HttpStatusCode.NotAcceptable:
848- case HttpStatusCode.NotFound:
849- case HttpStatusCode.PaymentRequired:
850- case HttpStatusCode.PreconditionFailed:
851- case HttpStatusCode.RequestedRangeNotSatisfiable:
852- case HttpStatusCode.RequestEntityTooLarge:
853- case HttpStatusCode.RequestTimeout:
854- case HttpStatusCode.RequestUriTooLong:
855- //仕様書にない400系エラー。サーバまでは到達しているのでリトライしない
856- return "Warn:" + res.ToString();
857- case HttpStatusCode.Unauthorized:
858- {
859- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
860- var errMsg = GetErrorMessageJson(content);
861- if (string.IsNullOrEmpty(errMsg))
862- {
863- return Properties.Resources.Unauthorized;
864- }
865- else
866- {
867- return "Auth err:" + errMsg;
868- }
869- }
870- default:
871- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
644+ if (op.Post(postStr.Length))
645+ {
646+ return "";
647+ }
648+ else
649+ {
650+ return "Outputz:Failed";
872651 }
873652 }
874653
@@ -878,8 +657,7 @@ namespace OpenTween
878657
879658 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
880659
881- HttpStatusCode res = HttpStatusCode.BadRequest;
882-
660+ HttpStatusCode res;
883661 try
884662 {
885663 res = twCon.DestroyStatus(id);
@@ -888,20 +666,8 @@ namespace OpenTween
888666 {
889667 return "Err:" + ex.Message;
890668 }
891-
892- switch (res)
893- {
894- case HttpStatusCode.OK:
895- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
896- return "";
897- case HttpStatusCode.Unauthorized:
898- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
899- return Properties.Resources.Unauthorized;
900- case HttpStatusCode.NotFound:
901- return "";
902- default:
903- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
904- }
669+
670+ return this.CheckStatusCode(res, null) ?? "";
905671 }
906672
907673 public string PostRetweet(long id, bool read)
@@ -921,7 +687,7 @@ namespace OpenTween
921687 target = TabInformations.GetInstance()[id].RetweetedId.Value; //再RTの場合は元発言をRT
922688 }
923689
924- HttpStatusCode res = HttpStatusCode.BadRequest;
690+ HttpStatusCode res;
925691 var content = "";
926692 try
927693 {
@@ -996,11 +762,11 @@ namespace OpenTween
996762 return "Auth Err:try to re-authorization.";
997763 }
998764
999- HttpStatusCode res = HttpStatusCode.BadRequest;
1000-
1001765 //if (post.IsMe)
1002766 // _deletemessages.Add(post)
1003767 //}
768+
769+ HttpStatusCode res;
1004770 try
1005771 {
1006772 res = twCon.DestroyDirectMessage(id);
@@ -1010,19 +776,7 @@ namespace OpenTween
1010776 return "Err:" + ex.Message;
1011777 }
1012778
1013- switch (res)
1014- {
1015- case HttpStatusCode.OK:
1016- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1017- return "";
1018- case HttpStatusCode.Unauthorized:
1019- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1020- return Properties.Resources.Unauthorized;
1021- case HttpStatusCode.NotFound:
1022- return "";
1023- default:
1024- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1025- }
779+ return this.CheckStatusCode(res, null) ?? "";
1026780 }
1027781
1028782 public string PostFollowCommand(string screenName)
@@ -1031,9 +785,8 @@ namespace OpenTween
1031785
1032786 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1033787
1034- HttpStatusCode res = HttpStatusCode.BadRequest;
788+ HttpStatusCode res;
1035789 var content = "";
1036-
1037790 try
1038791 {
1039792 res = twCon.CreateFriendships(screenName, ref content);
@@ -1043,27 +796,7 @@ namespace OpenTween
1043796 return "Err:" + ex.Message;
1044797 }
1045798
1046- switch (res)
1047- {
1048- case HttpStatusCode.OK:
1049- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1050- return "";
1051- case HttpStatusCode.Unauthorized:
1052- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1053- return Properties.Resources.Unauthorized;
1054- case HttpStatusCode.Forbidden:
1055- var errMsg = GetErrorMessageJson(content);
1056- if (string.IsNullOrEmpty(errMsg))
1057- {
1058- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1059- }
1060- else
1061- {
1062- return "Err:" + errMsg;
1063- }
1064- default:
1065- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1066- }
799+ return this.CheckStatusCode(res, content) ?? "";
1067800 }
1068801
1069802 public string PostRemoveCommand(string screenName)
@@ -1072,9 +805,8 @@ namespace OpenTween
1072805
1073806 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1074807
1075- HttpStatusCode res = HttpStatusCode.BadRequest;
808+ HttpStatusCode res;
1076809 var content = "";
1077-
1078810 try
1079811 {
1080812 res = twCon.DestroyFriendships(screenName, ref content);
@@ -1084,27 +816,7 @@ namespace OpenTween
1084816 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1085817 }
1086818
1087- switch (res)
1088- {
1089- case HttpStatusCode.OK:
1090- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1091- return "";
1092- case HttpStatusCode.Unauthorized:
1093- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1094- return Properties.Resources.Unauthorized;
1095- case HttpStatusCode.Forbidden:
1096- var errMsg = GetErrorMessageJson(content);
1097- if (string.IsNullOrEmpty(errMsg))
1098- {
1099- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1100- }
1101- else
1102- {
1103- return "Err:" + errMsg;
1104- }
1105- default:
1106- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1107- }
819+ return this.CheckStatusCode(res, content) ?? "";
1108820 }
1109821
1110822 public string PostCreateBlock(string screenName)
@@ -1113,9 +825,8 @@ namespace OpenTween
1113825
1114826 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1115827
1116- HttpStatusCode res = HttpStatusCode.BadRequest;
828+ HttpStatusCode res;
1117829 var content = "";
1118-
1119830 try
1120831 {
1121832 res = twCon.CreateBlock(screenName, ref content);
@@ -1125,27 +836,7 @@ namespace OpenTween
1125836 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1126837 }
1127838
1128- switch (res)
1129- {
1130- case HttpStatusCode.OK:
1131- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1132- return "";
1133- case HttpStatusCode.Unauthorized:
1134- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1135- return Properties.Resources.Unauthorized;
1136- case HttpStatusCode.Forbidden:
1137- var errMsg = GetErrorMessageJson(content);
1138- if (string.IsNullOrEmpty(errMsg))
1139- {
1140- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1141- }
1142- else
1143- {
1144- return "Err:" + errMsg;
1145- }
1146- default:
1147- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1148- }
839+ return this.CheckStatusCode(res, content) ?? "";
1149840 }
1150841
1151842 public string PostDestroyBlock(string screenName)
@@ -1154,9 +845,8 @@ namespace OpenTween
1154845
1155846 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1156847
1157- HttpStatusCode res = HttpStatusCode.BadRequest;
848+ HttpStatusCode res;
1158849 var content = "";
1159-
1160850 try
1161851 {
1162852 res = twCon.DestroyBlock(screenName, ref content);
@@ -1166,27 +856,7 @@ namespace OpenTween
1166856 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1167857 }
1168858
1169- switch (res)
1170- {
1171- case HttpStatusCode.OK:
1172- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1173- return "";
1174- case HttpStatusCode.Unauthorized:
1175- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1176- return Properties.Resources.Unauthorized;
1177- case HttpStatusCode.Forbidden:
1178- var errMsg = GetErrorMessageJson(content);
1179- if (string.IsNullOrEmpty(errMsg))
1180- {
1181- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1182- }
1183- else
1184- {
1185- return "Err:" + errMsg;
1186- }
1187- default:
1188- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1189- }
859+ return this.CheckStatusCode(res, content) ?? "";
1190860 }
1191861
1192862 public string PostReportSpam(string screenName)
@@ -1195,9 +865,8 @@ namespace OpenTween
1195865
1196866 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1197867
1198- HttpStatusCode res = HttpStatusCode.BadRequest;
868+ HttpStatusCode res;
1199869 var content = "";
1200-
1201870 try
1202871 {
1203872 res = twCon.ReportSpam(screenName, ref content);
@@ -1207,27 +876,7 @@ namespace OpenTween
1207876 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1208877 }
1209878
1210- switch (res)
1211- {
1212- case HttpStatusCode.OK:
1213- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1214- return "";
1215- case HttpStatusCode.Unauthorized:
1216- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1217- return Properties.Resources.Unauthorized;
1218- case HttpStatusCode.Forbidden:
1219- var errMsg = GetErrorMessageJson(content);
1220- if (string.IsNullOrEmpty(errMsg))
1221- {
1222- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1223- }
1224- else
1225- {
1226- return "Err:" + errMsg;
1227- }
1228- default:
1229- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1230- }
879+ return this.CheckStatusCode(res, content) ?? "";
1231880 }
1232881
1233882 public string GetFriendshipInfo(string screenName, ref bool isFollowing, ref bool isFollowed)
@@ -1236,7 +885,7 @@ namespace OpenTween
1236885
1237886 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1238887
1239- HttpStatusCode res = HttpStatusCode.BadRequest;
888+ HttpStatusCode res;
1240889 var content = "";
1241890 try
1242891 {
@@ -1247,33 +896,25 @@ namespace OpenTween
1247896 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1248897 }
1249898
1250- switch (res)
899+ var err = this.CheckStatusCode(res, content);
900+ if (err != null) return err;
901+
902+ try
1251903 {
1252- case HttpStatusCode.OK:
1253- try
1254- {
1255- var relation = MyCommon.CreateDataFromJson<TwitterDataModel.Relationship>(content);
1256- isFollowing = relation.relationship.Source.Following;
1257- isFollowed = relation.relationship.Source.FollowedBy;
1258- return "";
1259- }
1260- catch(SerializationException ex)
1261- {
1262- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1263- return "Err:Json Parse Error(DataContractJsonSerializer)";
1264- }
1265- catch(Exception ex)
1266- {
1267- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1268- return "Err:Invalid Json!";
1269- }
1270- case HttpStatusCode.BadRequest:
1271- return "Err:API Limits?";
1272- case HttpStatusCode.Unauthorized:
1273- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1274- return Properties.Resources.Unauthorized;
1275- default:
1276- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
904+ var relation = MyCommon.CreateDataFromJson<TwitterDataModel.Relationship>(content);
905+ isFollowing = relation.relationship.Source.Following;
906+ isFollowed = relation.relationship.Source.FollowedBy;
907+ return "";
908+ }
909+ catch(SerializationException ex)
910+ {
911+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
912+ return "Err:Json Parse Error(DataContractJsonSerializer)";
913+ }
914+ catch(Exception ex)
915+ {
916+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
917+ return "Err:Invalid Json!";
1277918 }
1278919 }
1279920
@@ -1283,9 +924,10 @@ namespace OpenTween
1283924
1284925 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
1285926
1286- HttpStatusCode res = HttpStatusCode.BadRequest;
1287- var content = "";
1288927 user = null;
928+
929+ HttpStatusCode res;
930+ var content = "";
1289931 try
1290932 {
1291933 res = twCon.ShowUserInfo(screenName, ref content);
@@ -1295,41 +937,24 @@ namespace OpenTween
1295937 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1296938 }
1297939
1298- switch (res)
940+ var err = this.CheckStatusCode(res, content);
941+ if (err != null) return err;
942+
943+ try
1299944 {
1300- case HttpStatusCode.OK:
1301- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1302- try
1303- {
1304- user = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content);
1305- }
1306- catch (SerializationException ex)
1307- {
1308- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1309- return "Err:Json Parse Error(DataContractJsonSerializer)";
1310- }
1311- catch (Exception ex)
1312- {
1313- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1314- return "Err:Invalid Json!";
1315- }
1316- return "";
1317- case HttpStatusCode.BadRequest:
1318- return "Err:API Limits?";
1319- case HttpStatusCode.Unauthorized:
1320- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1321- var errMsg = GetErrorMessageJson(content);
1322- if (string.IsNullOrEmpty(errMsg))
1323- {
1324- return Properties.Resources.Unauthorized;
1325- }
1326- else
1327- {
1328- return "Auth err:" + errMsg;
1329- }
1330- default:
1331- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
945+ user = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content);
946+ }
947+ catch (SerializationException ex)
948+ {
949+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
950+ return "Err:Json Parse Error(DataContractJsonSerializer)";
951+ }
952+ catch (Exception ex)
953+ {
954+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
955+ return "Err:Invalid Json!";
1332956 }
957+ return "";
1333958 }
1334959
1335960 public string GetStatus_Retweeted_Count(long StatusId, ref int retweeted_count)
@@ -1338,9 +963,8 @@ namespace OpenTween
1338963
1339964 if (MyCommon._endingFlag) return "";
1340965
1341- HttpStatusCode res = HttpStatusCode.BadRequest;
966+ HttpStatusCode res;
1342967 var content = "";
1343-
1344968 try
1345969 {
1346970 res = twCon.ShowStatuses(StatusId, ref content);
@@ -1349,21 +973,9 @@ namespace OpenTween
1349973 {
1350974 return "Err:" + ex.Message;
1351975 }
1352- switch (res)
1353- {
1354- case HttpStatusCode.OK:
1355- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1356- break;
1357- case HttpStatusCode.Unauthorized:
1358- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1359- return Properties.Resources.Unauthorized;
1360- case HttpStatusCode.BadRequest:
1361- return "Err:API Limits?";
1362- case HttpStatusCode.Forbidden:
1363- return "Err:protected user's tweet";
1364- default:
1365- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1366- }
976+
977+ var err = this.CheckStatusCode(res, content);
978+ if (err != null) return err;
1367979
1368980 TwitterDataModel.Status status;
1369981 try
@@ -1394,7 +1006,7 @@ namespace OpenTween
13941006
13951007 //if (this.favQueue.Contains(id)) this.favQueue.Remove(id)
13961008
1397- HttpStatusCode res = HttpStatusCode.BadRequest;
1009+ HttpStatusCode res;
13981010 var content = "";
13991011 try
14001012 {
@@ -1407,41 +1019,13 @@ namespace OpenTween
14071019 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
14081020 }
14091021
1410- switch (res)
1411- {
1412- case HttpStatusCode.OK:
1413- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1414- //this.favQueue.FavoriteCacheStart();
1415- if (!_restrictFavCheck) return "";
1416- break;
1417- case HttpStatusCode.Unauthorized:
1418- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1419- return Properties.Resources.Unauthorized;
1420- case HttpStatusCode.Forbidden:
1421- var errMsg = GetErrorMessageJson(content);
1422- if (string.IsNullOrEmpty(errMsg))
1423- {
1424- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1425- }
1426- else
1427- {
1428- //if (errMsg.Contains("It's great that you like so many updates"))
1429- // //this.favQueue.Add(id)
1430- // return "Err:->FavoriteQueue:" + errMsg;
1431- //}
1432- return "Err:" + errMsg;
1433- }
1434- //Case HttpStatusCode.BadGateway, HttpStatusCode.ServiceUnavailable, HttpStatusCode.InternalServerError, HttpStatusCode.RequestTimeout
1435- // //this.favQueue.Add(id)
1436- // return "Err:->FavoriteQueue:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1437- default:
1438- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1439- }
1022+ var err = this.CheckStatusCode(res, content);
1023+ if (err != null) return err;
1024+
1025+ if (!_restrictFavCheck) return "";
14401026
14411027 //http://twitter.com/statuses/show/id.xml APIを発行して本文を取得
14421028
1443- //var content = "";
1444- content = "";
14451029 try
14461030 {
14471031 res = twCon.ShowStatuses(id, ref content);
@@ -1451,40 +1035,31 @@ namespace OpenTween
14511035 return "Err:" + ex.Message;
14521036 }
14531037
1454- switch (res)
1038+ err = this.CheckStatusCode(res, content);
1039+ if (err != null) return err;
1040+
1041+ TwitterDataModel.Status status;
1042+ try
14551043 {
1456- case HttpStatusCode.OK:
1457- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1458- TwitterDataModel.Status status;
1459- try
1460- {
1461- status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
1462- }
1463- catch (SerializationException ex)
1464- {
1465- MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1466- return "Err:Json Parse Error(DataContractJsonSerializer)";
1467- }
1468- catch (Exception ex)
1469- {
1470- MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1471- return "Err:Invalid Json!";
1472- }
1473- if (status.Favorited)
1474- {
1475- return "";
1476- }
1477- else
1478- {
1479- return "NG(Restricted?)";
1480- }
1481- case HttpStatusCode.Unauthorized:
1482- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1483- return Properties.Resources.Unauthorized;
1484- case HttpStatusCode.BadRequest:
1485- return "Err:API Limits?";
1486- default:
1487- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1044+ status = MyCommon.CreateDataFromJson<TwitterDataModel.Status>(content);
1045+ }
1046+ catch (SerializationException ex)
1047+ {
1048+ MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
1049+ return "Err:Json Parse Error(DataContractJsonSerializer)";
1050+ }
1051+ catch (Exception ex)
1052+ {
1053+ MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
1054+ return "Err:Invalid Json!";
1055+ }
1056+ if (status.Favorited)
1057+ {
1058+ return "";
1059+ }
1060+ else
1061+ {
1062+ return "NG(Restricted?)";
14881063 }
14891064 }
14901065
@@ -1501,7 +1076,7 @@ namespace OpenTween
15011076 // return "";
15021077 //}
15031078
1504- HttpStatusCode res = HttpStatusCode.BadRequest;
1079+ HttpStatusCode res;
15051080 var content = "";
15061081 try
15071082 {
@@ -1512,27 +1087,7 @@ namespace OpenTween
15121087 return "Err:" + ex.Message;
15131088 }
15141089
1515- switch (res)
1516- {
1517- case HttpStatusCode.OK:
1518- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1519- return "";
1520- case HttpStatusCode.Unauthorized:
1521- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1522- return Properties.Resources.Unauthorized;
1523- case HttpStatusCode.Forbidden:
1524- var errMsg = GetErrorMessageJson(content);
1525- if (string.IsNullOrEmpty(errMsg))
1526- {
1527- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1528- }
1529- else
1530- {
1531- return "Err:" + errMsg;
1532- }
1533- default:
1534- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1535- }
1090+ return this.CheckStatusCode(res, content) ?? "";
15361091 }
15371092
15381093 public string PostUpdateProfile(string name, string url, string location, string description)
@@ -1541,7 +1096,7 @@ namespace OpenTween
15411096
15421097 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
15431098
1544- HttpStatusCode res = HttpStatusCode.BadRequest;
1099+ HttpStatusCode res;
15451100 var content = "";
15461101 try
15471102 {
@@ -1552,27 +1107,7 @@ namespace OpenTween
15521107 return "Err:" + ex.Message;
15531108 }
15541109
1555- switch (res)
1556- {
1557- case HttpStatusCode.OK:
1558- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1559- return "";
1560- case HttpStatusCode.Unauthorized:
1561- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1562- return Properties.Resources.Unauthorized;
1563- case HttpStatusCode.Forbidden:
1564- var errMsg = GetErrorMessageJson(content);
1565- if (string.IsNullOrEmpty(errMsg))
1566- {
1567- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1568- }
1569- else
1570- {
1571- return "Err:" + errMsg;
1572- }
1573- default:
1574- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1575- }
1110+ return this.CheckStatusCode(res, content) ?? "";
15761111 }
15771112
15781113 public string PostUpdateProfileImage(string filename)
@@ -1581,38 +1116,18 @@ namespace OpenTween
15811116
15821117 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
15831118
1584- HttpStatusCode res = HttpStatusCode.BadRequest;
1119+ HttpStatusCode res;
15851120 var content = "";
15861121 try
15871122 {
1588- res = twCon.UpdateProfileImage(new FileInfo(filename), ref content);
1589- }
1590- catch(Exception ex)
1591- {
1592- return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
1593- }
1594-
1595- switch (res)
1596- {
1597- case HttpStatusCode.OK:
1598- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1599- return "";
1600- case HttpStatusCode.Unauthorized:
1601- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1602- return Properties.Resources.Unauthorized;
1603- case HttpStatusCode.Forbidden:
1604- var errMsg = GetErrorMessageJson(content);
1605- if (string.IsNullOrEmpty(errMsg))
1606- {
1607- return "Err:Forbidden(" + MethodBase.GetCurrentMethod().Name + ")";
1608- }
1609- else
1610- {
1611- return "Err:" + errMsg;
1612- }
1613- default:
1614- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1123+ res = twCon.UpdateProfileImage(new FileInfo(filename), ref content);
1124+ }
1125+ catch(Exception ex)
1126+ {
1127+ return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
16151128 }
1129+
1130+ return this.CheckStatusCode(res, content) ?? "";
16161131 }
16171132
16181133 public string Username
@@ -1919,19 +1434,9 @@ namespace OpenTween
19191434 {
19201435 return "Err:" + ex.Message;
19211436 }
1922- switch (res)
1923- {
1924- case HttpStatusCode.OK:
1925- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1926- break;
1927- case HttpStatusCode.Unauthorized:
1928- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
1929- return Properties.Resources.Unauthorized;
1930- case HttpStatusCode.BadRequest:
1931- return "Err:API Limits?";
1932- default:
1933- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1934- }
1437+
1438+ var err = this.CheckStatusCode(res, content);
1439+ if (err != null) return err;
19351440
19361441 if (gType == MyCommon.WORKERTYPE.Timeline)
19371442 {
@@ -1953,7 +1458,7 @@ namespace OpenTween
19531458
19541459 if (MyCommon._endingFlag) return "";
19551460
1956- HttpStatusCode res = HttpStatusCode.BadRequest;
1461+ HttpStatusCode res;
19571462 var content = "";
19581463
19591464 if (count == 0) count = 20;
@@ -1982,19 +1487,12 @@ namespace OpenTween
19821487 {
19831488 return "Err:" + ex.Message;
19841489 }
1985- switch (res)
1986- {
1987- case HttpStatusCode.OK:
1988- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1989- break;
1990- case HttpStatusCode.Unauthorized:
1991- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
1992- return "Err:@" + userName + "'s Tweets are protected.";
1993- case HttpStatusCode.BadRequest:
1994- return "Err:API Limits?";
1995- default:
1996- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
1997- }
1490+
1491+ if (res == HttpStatusCode.Unauthorized)
1492+ return "Err:@" + userName + "'s Tweets are protected.";
1493+
1494+ var err = this.CheckStatusCode(res, content);
1495+ if (err != null) return err;
19981496
19991497 List<TwitterDataModel.Status> items;
20001498 try
@@ -2035,9 +1533,8 @@ namespace OpenTween
20351533
20361534 if (MyCommon._endingFlag) return "";
20371535
2038- HttpStatusCode res = HttpStatusCode.BadRequest;
1536+ HttpStatusCode res;
20391537 var content = "";
2040-
20411538 try
20421539 {
20431540 res = twCon.ShowStatuses(id, ref content);
@@ -2046,21 +1543,12 @@ namespace OpenTween
20461543 {
20471544 return "Err:" + ex.Message;
20481545 }
2049- switch (res)
2050- {
2051- case HttpStatusCode.OK:
2052- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2053- break;
2054- case HttpStatusCode.Unauthorized:
2055- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2056- return Properties.Resources.Unauthorized;
2057- case HttpStatusCode.BadRequest:
2058- return "Err:API Limits?";
2059- case HttpStatusCode.Forbidden:
2060- return "Err:protected user's tweet";
2061- default:
2062- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
2063- }
1546+
1547+ if (res == HttpStatusCode.Forbidden)
1548+ return "Err:protected user's tweet";
1549+
1550+ var err = this.CheckStatusCode(res, content);
1551+ if (err != null) return err;
20641552
20651553 TwitterDataModel.Status status;
20661554 try
@@ -2509,19 +1997,9 @@ namespace OpenTween
25091997 {
25101998 return "Err:" + ex.Message;
25111999 }
2512- switch (res)
2513- {
2514- case HttpStatusCode.OK:
2515- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2516- break;
2517- case HttpStatusCode.Unauthorized:
2518- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2519- return Properties.Resources.Unauthorized;
2520- case HttpStatusCode.BadRequest:
2521- return "Err:API Limits?";
2522- default:
2523- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
2524- }
2000+
2001+ var err = this.CheckStatusCode(res, content);
2002+ if (err != null) return err;
25252003
25262004 return CreatePostsFromJson(content, MyCommon.WORKERTYPE.List, tab, read, count, ref tab.OldestId);
25272005 }
@@ -2659,7 +2137,7 @@ namespace OpenTween
26592137
26602138 if (MyCommon._endingFlag) return "";
26612139
2662- HttpStatusCode res = HttpStatusCode.BadRequest;
2140+ HttpStatusCode res;
26632141 var content = "";
26642142 try
26652143 {
@@ -2676,19 +2154,9 @@ namespace OpenTween
26762154 {
26772155 return "Err:" + ex.Message;
26782156 }
2679- switch (res)
2680- {
2681- case HttpStatusCode.OK:
2682- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
2683- break;
2684- case HttpStatusCode.Unauthorized:
2685- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
2686- return Properties.Resources.Unauthorized;
2687- case HttpStatusCode.BadRequest:
2688- return "Err:API Limits?";
2689- default:
2690- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
2691- }
2157+
2158+ var err = this.CheckStatusCode(res, content);
2159+ if (err != null) return err;
26922160
26932161 List<TwitterDataModel.RelatedResult> items;
26942162 try
@@ -2987,7 +2455,7 @@ namespace OpenTween
29872455 return "Auth Err:try to re-authorization.";
29882456 }
29892457
2990- HttpStatusCode res = HttpStatusCode.BadRequest;
2458+ HttpStatusCode res;
29912459 var content = "";
29922460
29932461 try
@@ -3020,19 +2488,8 @@ namespace OpenTween
30202488 return "Err:" + ex.Message;
30212489 }
30222490
3023- switch (res)
3024- {
3025- case HttpStatusCode.OK:
3026- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3027- break;
3028- case HttpStatusCode.Unauthorized:
3029- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3030- return Properties.Resources.Unauthorized;
3031- case HttpStatusCode.BadRequest:
3032- return "Err:API Limits?";
3033- default:
3034- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3035- }
2491+ var err = this.CheckStatusCode(res, content);
2492+ if (err != null) return err;
30362493
30372494 return CreateDirectMessagesFromJson(content, gType, read);
30382495 }
@@ -3046,8 +2503,6 @@ namespace OpenTween
30462503
30472504 if (MyCommon._endingFlag) return "";
30482505
3049- HttpStatusCode res;
3050- var content = "";
30512506 var count = AppendSettingDialog.Instance.CountApi;
30522507 if (AppendSettingDialog.Instance.UseAdditionalCount &&
30532508 AppendSettingDialog.Instance.FavoritesCountApi != 0)
@@ -3065,6 +2520,8 @@ namespace OpenTween
30652520 page_ = 1;
30662521 }
30672522
2523+ HttpStatusCode res;
2524+ var content = "";
30682525 try
30692526 {
30702527 res = twCon.Favorites(count, page_, ref content);
@@ -3074,23 +2531,10 @@ namespace OpenTween
30742531 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
30752532 }
30762533
3077- switch (res)
3078- {
3079- case HttpStatusCode.OK:
3080- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3081- break;
3082- case HttpStatusCode.Unauthorized:
3083- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3084- return Properties.Resources.Unauthorized;
3085- case HttpStatusCode.BadRequest:
3086- return "Err:API Limits?";
3087- default:
3088- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3089- }
2534+ var err = this.CheckStatusCode(res, content);
2535+ if (err != null) return err;
30902536
3091- var serializer = new DataContractJsonSerializer(typeof(List<TwitterDataModel.Status>));
30922537 List<TwitterDataModel.Status> item;
3093-
30942538 try
30952539 {
30962540 item = MyCommon.CreateDataFromJson<List<TwitterDataModel.Status>>(content);
@@ -3271,7 +2715,7 @@ namespace OpenTween
32712715 {
32722716 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
32732717
3274- HttpStatusCode res = HttpStatusCode.BadRequest;
2718+ HttpStatusCode res;
32752719 var content = "";
32762720 try
32772721 {
@@ -3282,19 +2726,8 @@ namespace OpenTween
32822726 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
32832727 }
32842728
3285- switch (res)
3286- {
3287- case HttpStatusCode.OK:
3288- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3289- break;
3290- case HttpStatusCode.Unauthorized:
3291- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3292- return Properties.Resources.Unauthorized;
3293- case HttpStatusCode.BadRequest:
3294- return "Err:API Limits?";
3295- default:
3296- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3297- }
2729+ var err = this.CheckStatusCode(res, content);
2730+ if (err != null) return err;
32982731
32992732 try
33002733 {
@@ -3341,7 +2774,7 @@ namespace OpenTween
33412774 {
33422775 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
33432776
3344- HttpStatusCode res = HttpStatusCode.BadRequest;
2777+ HttpStatusCode res;
33452778 var content = "";
33462779 try
33472780 {
@@ -3352,19 +2785,8 @@ namespace OpenTween
33522785 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
33532786 }
33542787
3355- switch (res)
3356- {
3357- case HttpStatusCode.OK:
3358- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3359- break;
3360- case HttpStatusCode.Unauthorized:
3361- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3362- return Properties.Resources.Unauthorized;
3363- case HttpStatusCode.BadRequest:
3364- return "Err:API Limits?";
3365- default:
3366- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3367- }
2788+ var err = this.CheckStatusCode(res, content);
2789+ if (err != null) return err;
33682790
33692791 try
33702792 {
@@ -3406,19 +2828,8 @@ namespace OpenTween
34062828 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
34072829 }
34082830
3409- switch (res)
3410- {
3411- case HttpStatusCode.OK:
3412- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3413- break;
3414- case HttpStatusCode.Unauthorized:
3415- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3416- return Properties.Resources.Unauthorized;
3417- case HttpStatusCode.BadRequest:
3418- return "Err:API Limits?";
3419- default:
3420- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3421- }
2831+ var err = this.CheckStatusCode(res, content);
2832+ if (err != null) return err;
34222833
34232834 try
34242835 {
@@ -3446,7 +2857,7 @@ namespace OpenTween
34462857 {
34472858 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
34482859
3449- HttpStatusCode res = HttpStatusCode.BadRequest;
2860+ HttpStatusCode res;
34502861 var content = "";
34512862 long cursor = -1;
34522863
@@ -3462,19 +2873,8 @@ namespace OpenTween
34622873 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
34632874 }
34642875
3465- switch (res)
3466- {
3467- case HttpStatusCode.OK:
3468- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3469- break;
3470- case HttpStatusCode.Unauthorized:
3471- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3472- return Properties.Resources.Unauthorized;
3473- case HttpStatusCode.BadRequest:
3474- return "Err:API Limits?";
3475- default:
3476- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3477- }
2876+ var err = this.CheckStatusCode(res, content);
2877+ if (err != null) return err;
34782878
34792879 try
34802880 {
@@ -3495,7 +2895,6 @@ namespace OpenTween
34952895 } while (cursor != 0);
34962896
34972897 cursor = -1;
3498- content = "";
34992898 do
35002899 {
35012900 try
@@ -3507,19 +2906,8 @@ namespace OpenTween
35072906 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
35082907 }
35092908
3510- switch (res)
3511- {
3512- case HttpStatusCode.OK:
3513- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3514- break;
3515- case HttpStatusCode.Unauthorized:
3516- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3517- return Properties.Resources.Unauthorized;
3518- case HttpStatusCode.BadRequest:
3519- return "Err:API Limits?";
3520- default:
3521- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3522- }
2909+ var err = this.CheckStatusCode(res, content);
2910+ if (err != null) return err;
35232911
35242912 try
35252913 {
@@ -3547,7 +2935,7 @@ namespace OpenTween
35472935 {
35482936 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
35492937
3550- HttpStatusCode res = HttpStatusCode.BadRequest;
2938+ HttpStatusCode res;
35512939 IEnumerable<ListElement> lists;
35522940 var content = "";
35532941
@@ -3560,19 +2948,8 @@ namespace OpenTween
35602948 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
35612949 }
35622950
3563- switch (res)
3564- {
3565- case HttpStatusCode.OK:
3566- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3567- break;
3568- case HttpStatusCode.Unauthorized:
3569- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3570- return Properties.Resources.Unauthorized;
3571- case HttpStatusCode.BadRequest:
3572- return "Err:API Limits?";
3573- default:
3574- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3575- }
2951+ var err = this.CheckStatusCode(res, content);
2952+ if (err != null) return err;
35762953
35772954 try
35782955 {
@@ -3590,7 +2967,6 @@ namespace OpenTween
35902967 return "Err:Invalid Json!";
35912968 }
35922969
3593- content = "";
35942970 try
35952971 {
35962972 res = twCon.GetListsSubscriptions(this.Username, null, ref content);
@@ -3600,19 +2976,8 @@ namespace OpenTween
36002976 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
36012977 }
36022978
3603- switch (res)
3604- {
3605- case HttpStatusCode.OK:
3606- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3607- break;
3608- case HttpStatusCode.Unauthorized:
3609- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3610- return Properties.Resources.Unauthorized;
3611- case HttpStatusCode.BadRequest:
3612- return "Err:API Limits?";
3613- default:
3614- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3615- }
2979+ err = this.CheckStatusCode(res, content);
2980+ if (err != null) return err;
36162981
36172982 try
36182983 {
@@ -3636,7 +3001,7 @@ namespace OpenTween
36363001
36373002 public string DeleteList(string list_id)
36383003 {
3639- HttpStatusCode res = HttpStatusCode.BadRequest;
3004+ HttpStatusCode res;
36403005 var content = "";
36413006
36423007 try
@@ -3648,26 +3013,15 @@ namespace OpenTween
36483013 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
36493014 }
36503015
3651- switch (res)
3652- {
3653- case HttpStatusCode.OK:
3654- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3655- break;
3656- case HttpStatusCode.Unauthorized:
3657- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3658- return Properties.Resources.Unauthorized;
3659- case HttpStatusCode.BadRequest:
3660- return "Err:API Limits?";
3661- default:
3662- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3663- }
3016+ var err = this.CheckStatusCode(res, content);
3017+ if (err != null) return err;
36643018
36653019 return "";
36663020 }
36673021
36683022 public string EditList(string list_id, string new_name, bool isPrivate, string description, ref ListElement list)
36693023 {
3670- HttpStatusCode res = HttpStatusCode.BadRequest;
3024+ HttpStatusCode res;
36713025 var content = "";
36723026
36733027 try
@@ -3679,19 +3033,8 @@ namespace OpenTween
36793033 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
36803034 }
36813035
3682- switch (res)
3683- {
3684- case HttpStatusCode.OK:
3685- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3686- break;
3687- case HttpStatusCode.Unauthorized:
3688- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3689- return Properties.Resources.Unauthorized;
3690- case HttpStatusCode.BadRequest:
3691- return "Err:API Limits?";
3692- default:
3693- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3694- }
3036+ var err = this.CheckStatusCode(res, content);
3037+ if (err != null) return err;
36953038
36963039 try
36973040 {
@@ -3726,10 +3069,8 @@ namespace OpenTween
37263069 {
37273070 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
37283071
3729- HttpStatusCode res = HttpStatusCode.BadRequest;
3072+ HttpStatusCode res;
37303073 var content = "";
3731-
3732- //Do
37333074 try
37343075 {
37353076 res = twCon.GetListMembers(this.Username, list_id, cursor, ref content);
@@ -3739,19 +3080,8 @@ namespace OpenTween
37393080 return "Err:" + ex.Message;
37403081 }
37413082
3742- switch (res)
3743- {
3744- case HttpStatusCode.OK:
3745- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3746- break;
3747- case HttpStatusCode.Unauthorized:
3748- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3749- return Properties.Resources.Unauthorized;
3750- case HttpStatusCode.BadRequest:
3751- return "Err:API Limits?";
3752- default:
3753- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3754- }
3083+ var err = this.CheckStatusCode(res, content);
3084+ if (err != null) return err;
37553085
37563086 try
37573087 {
@@ -3778,9 +3108,8 @@ namespace OpenTween
37783108 {
37793109 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
37803110
3781- HttpStatusCode res = HttpStatusCode.BadRequest;
3111+ HttpStatusCode res;
37823112 var content = "";
3783-
37843113 try
37853114 {
37863115 res = twCon.CreateLists(listName, isPrivate, description, ref content);
@@ -3790,19 +3119,8 @@ namespace OpenTween
37903119 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
37913120 }
37923121
3793- switch (res)
3794- {
3795- case HttpStatusCode.OK:
3796- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3797- break;
3798- case HttpStatusCode.Unauthorized:
3799- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3800- return Properties.Resources.Unauthorized;
3801- case HttpStatusCode.BadRequest:
3802- return "Err:API Limits?";
3803- default:
3804- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3805- }
3122+ var err = this.CheckStatusCode(res, content);
3123+ if (err != null) return err;
38063124
38073125 try
38083126 {
@@ -3828,7 +3146,7 @@ namespace OpenTween
38283146
38293147 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
38303148
3831- HttpStatusCode res = HttpStatusCode.BadRequest;
3149+ HttpStatusCode res;
38323150 var content = "";
38333151
38343152 try
@@ -3840,23 +3158,15 @@ namespace OpenTween
38403158 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
38413159 }
38423160
3843- switch (res)
3161+ if (res == HttpStatusCode.NotFound)
38443162 {
3845- case HttpStatusCode.OK:
3846- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3847- break;
3848- case HttpStatusCode.Unauthorized:
3849- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3850- return Properties.Resources.Unauthorized;
3851- case HttpStatusCode.BadRequest:
3852- return "Err:API Limits?";
3853- case HttpStatusCode.NotFound:
3854- value = false;
3855- return "";
3856- default:
3857- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3163+ value = false;
3164+ return "";
38583165 }
38593166
3167+ var err = this.CheckStatusCode(res, content);
3168+ if (err != null) return err;
3169+
38603170 try
38613171 {
38623172 var u = MyCommon.CreateDataFromJson<TwitterDataModel.User>(content);
@@ -3872,8 +3182,8 @@ namespace OpenTween
38723182
38733183 public string AddUserToList(string listId, string user)
38743184 {
3185+ HttpStatusCode res;
38753186 var content = "";
3876- HttpStatusCode res = HttpStatusCode.BadRequest;
38773187
38783188 try
38793189 {
@@ -3884,28 +3194,16 @@ namespace OpenTween
38843194 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
38853195 }
38863196
3887- switch (res)
3888- {
3889- case HttpStatusCode.OK:
3890- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3891- break;
3892- case HttpStatusCode.Unauthorized:
3893- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3894- return Properties.Resources.Unauthorized;
3895- case HttpStatusCode.BadRequest:
3896- return "Err:" + GetErrorMessageJson(content);
3897- default:
3898- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3899- }
3197+ var err = this.CheckStatusCode(res, content);
3198+ if (err != null) return err;
39003199
39013200 return "";
39023201 }
39033202
39043203 public string RemoveUserToList(string listId, string user)
39053204 {
3906-
3205+ HttpStatusCode res;
39073206 var content = "";
3908- HttpStatusCode res = HttpStatusCode.BadRequest;
39093207
39103208 try
39113209 {
@@ -3916,19 +3214,8 @@ namespace OpenTween
39163214 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
39173215 }
39183216
3919- switch (res)
3920- {
3921- case HttpStatusCode.OK:
3922- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3923- break;
3924- case HttpStatusCode.Unauthorized:
3925- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3926- return Properties.Resources.Unauthorized;
3927- case HttpStatusCode.BadRequest:
3928- return "Err:" + GetErrorMessageJson(content);
3929- default:
3930- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
3931- }
3217+ var err = this.CheckStatusCode(res, content);
3218+ if (err != null) return err;
39323219
39333220 return "";
39343221 }
@@ -4231,7 +3518,7 @@ namespace OpenTween
42313518
42323519 if (MyCommon._endingFlag) return null;
42333520
4234- HttpStatusCode res = HttpStatusCode.BadRequest;
3521+ HttpStatusCode res;
42353522 var content = "";
42363523 try
42373524 {
@@ -4243,7 +3530,8 @@ namespace OpenTween
42433530 return null;
42443531 }
42453532
4246- if (res != HttpStatusCode.OK) return null;
3533+ var err = this.CheckStatusCode(res, content);
3534+ if (err != null) return null;
42473535
42483536 try
42493537 {
@@ -4266,7 +3554,7 @@ namespace OpenTween
42663554
42673555 if (MyCommon._endingFlag) return null;
42683556
4269- HttpStatusCode res = HttpStatusCode.BadRequest;
3557+ HttpStatusCode res;
42703558 var content = "";
42713559 try
42723560 {
@@ -4278,7 +3566,8 @@ namespace OpenTween
42783566 return null;
42793567 }
42803568
4281- if (res != HttpStatusCode.OK) return null;
3569+ var err = this.CheckStatusCode(res, content);
3570+ if (err != null) return null;
42823571
42833572 try
42843573 {
@@ -4297,7 +3586,7 @@ namespace OpenTween
42973586 {
42983587 if (Twitter.AccountState != MyCommon.ACCOUNT_STATE.Valid) return "";
42993588
4300- HttpStatusCode res = HttpStatusCode.BadRequest;
3589+ HttpStatusCode res;
43013590 var content = "";
43023591
43033592 try
@@ -4309,19 +3598,8 @@ namespace OpenTween
43093598 return "Err:" + ex.Message + "(" + MethodBase.GetCurrentMethod().Name + ")";
43103599 }
43113600
4312- switch (res)
4313- {
4314- case HttpStatusCode.OK:
4315- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
4316- break;
4317- case HttpStatusCode.Unauthorized:
4318- Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
4319- return Properties.Resources.Unauthorized;
4320- case HttpStatusCode.BadRequest:
4321- return "Err:API Limits?";
4322- default:
4323- return "Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")";
4324- }
3601+ var err = this.CheckStatusCode(res, content);
3602+ if (err != null) return err;
43253603
43263604 try
43273605 {
@@ -4370,6 +3648,48 @@ namespace OpenTween
43703648 }
43713649 }
43723650
3651+ [MethodImpl(MethodImplOptions.NoInlining)] // 呼び出し元の取得に必要
3652+ private string CheckStatusCode(HttpStatusCode httpStatus, string responseText)
3653+ {
3654+ if (httpStatus == HttpStatusCode.OK)
3655+ {
3656+ Twitter.AccountState = MyCommon.ACCOUNT_STATE.Valid;
3657+ return null;
3658+ }
3659+
3660+ // 404エラーの挙動が変なので無視: https://dev.twitter.com/discussions/1213
3661+ if (httpStatus == HttpStatusCode.NotFound) return null;
3662+
3663+ var callerMethod = new StackTrace(false).GetFrame(1).GetMethod().Name;
3664+
3665+ if (string.IsNullOrWhiteSpace(responseText))
3666+ {
3667+ if (httpStatus == HttpStatusCode.Unauthorized)
3668+ Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3669+
3670+ return "Err:" + httpStatus + "(" + callerMethod + ")";
3671+ }
3672+
3673+ try
3674+ {
3675+ var errors = MyCommon.CreateDataFromJson<TwitterDataModel.ErrorResponse>(responseText).Errors;
3676+
3677+ foreach (var error in errors)
3678+ {
3679+ if (error.Code == TwitterDataModel.ErrorCode.InvalidToken ||
3680+ error.Code == TwitterDataModel.ErrorCode.SuspendedAccount)
3681+ {
3682+ Twitter.AccountState = MyCommon.ACCOUNT_STATE.Invalid;
3683+ }
3684+ }
3685+
3686+ return "Err:" + string.Join(",", errors.Select(x => x.ToString())) + "(" + callerMethod + ")";
3687+ }
3688+ catch (SerializationException) { }
3689+
3690+ return "Err:" + responseText + "(" + callerMethod + ")";
3691+ }
3692+
43733693 #region "UserStream"
43743694 private string trackWord_ = "";
43753695 public string TrackWord