catchで例外処理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="nx">request</span> <span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="dl">"</span><span class="s2">/getpodstatus</span><span class="dl">"</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)</span> <span class="c1">// やりたい処理</span> <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">request</span> <span class="o">=></span> <span class="p">{</span> <span class="k">this</span><span class="p">.</span><span class="nx">setState</span><span class="p">({</span> <span class="na">podstatus</span><span class="p">:</span> <span class="nx">request</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">items</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">status</span><span class="p">.</span><span class="nx">phase</span> <span class="p">});</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">state</span><span class="p">.</span><span class="nx">podstatus</span><span class="p">)</span> <span class="p">}</span> <span class="p">)</span> <span class="c1">// エラー時というかやりたい処理ができなかった場合の処理</span> <span class="p">.</span><span class="k">catch</span><span class="p">(</span><span class="nx">error</span> <span class="o">=></span> <span class="p">{</span> <span class="k">this</span><span class="p">.</span><span class="nx">setState</span><span class="p">({</span> <span class="na">podstatus</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Stop</span><span class="dl">'</span> <span class="p">});</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">POST失敗時の処理</span><span class="dl">'</span><span class="p">)</span> <span class="p">})</span> |