Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1131683 of runtime.lastError

  • Revision slug: Mozilla/Add-ons/WebExtensions/API/runtime/lastError
  • Revision title: runtime.lastError
  • Revision id: 1131683
  • Created:
  • Creator: wbamberg
  • Is current revision? Yes
  • Comment

Revision Content

{{AddonSidebar()}}
This value is set when an asynchronous function has an error condition that it needs to report to its caller.
 
If you call an asynchronous function that may set lastError, you are expected to check for the error when you handle the result of the function. If lastError has been set and you don't check it within the callback function, then an error will be raised.
 

Syntax

var myError = chrome.runtime.lastError;  // null or Error object

This API is also available as browser.runtime.lastError.

Value

An Error object representing the error. The message property is a string with a human-readable description of the error . If lastError has not been set, the value is null.

Examples

There are two different ways add-ons can call asynchronous functions:

  • By passing in a callback to the function, which is called with the function's result.
  • By omitting the callback and using the browser namespace, in which case the function will return a Promise.

In the first case, if the function can set lastError you should check it in the callback. For example:

function logCookie(c) {
  if (chrome.runtime.lastError) {
    console.error(chrome.runtime.lastError);
  } else {
    console.log(c);
  }
}

chrome.cookies.set(
  {url: "https://developer.mozilla.org/"},
  logCookie
);

In the second case, lastError will be passed into the second argument of Promise.then():

function logCookie(c) {
  console.log(c);
}

function logError(e) {
  console.error(e);
}

var setCookie = browser.cookies.set(
  {url: "https://developer.mozilla.org/"}
);

setCookie.then(logCookie, logError);

Note: runtime.lastError is an alias for {{WebExtAPIRef("extension.lastError")}}: they are set together, and checking either one will work.

Browser compatibility

{{WebExtBrowserCompat}}

{{WebExtExamples}}

Acknowledgements

This API is based on Chromium's chrome.runtime API. This documentation is derived from runtime.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Revision Source

<div>{{AddonSidebar()}}</div>

<div>This value is set when an asynchronous function has an error condition that it needs to report to its caller.</div>

<div>&nbsp;</div>

<div>If you call an asynchronous function that may set <code>lastError</code>, you are expected to check for the error when you handle the result of the function. If <code>lastError</code> has been set and you don't check it within the callback function, then an error will be raised.</div>

<div>&nbsp;</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox brush:js">
var myError = chrome.runtime.lastError;  // null or Error object</pre>

<p>This API is also available as <code>browser.runtime.lastError</code>.</p>

<h3 id="Value">Value</h3>

<p>An <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a> object representing the error. The <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message"><code>message</code></a> property is a <code>string</code> with a human-readable description of the error . If <code>lastError</code> has not been set, the value is <code>null</code>.</p>

<h2 id="Examples">Examples</h2>

<p>There are two different ways add-ons can call asynchronous functions:</p>

<ul>
 <li>By passing in a callback to the function, which is called with the function's result.</li>
 <li>By omitting the callback and using the <code>browser</code> namespace, in which case the function will return a <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>.</li>
</ul>

<p>In the first case, if the function can set <code>lastError</code> you should check it in the callback. For example:</p>

<pre class="brush: js line-numbers language-js">
<code class="language-js"><span class="keyword token">function</span> <span class="function token">logCookie</span><span class="punctuation token">(</span>c<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  <span class="keyword token">if</span> <span class="punctuation token">(</span>chrome<span class="punctuation token">.runtime</span><span class="punctuation token">.</span>lastError<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    console<span class="punctuation token">.</span><span class="function token">error</span><span class="punctuation token">(chrome</span><span class="punctuation token">.runtime</span><span class="punctuation token">.</span>lastError<span class="punctuation token">)</span><span class="punctuation token">;</span>
  <span class="punctuation token">}</span> <span class="keyword token">else</span> <span class="punctuation token">{</span>
    console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>c<span class="punctuation token">)</span><span class="punctuation token">;</span>
  <span class="punctuation token">}</span>
<span class="punctuation token">}</span>

chrome<span class="punctuation token">.</span>cookies<span class="punctuation token">.</span><span class="keyword token">set</span><span class="punctuation token">(</span>
  <span class="punctuation token">{</span>url<span class="punctuation token">:</span> <span class="string token">"https://developer.mozilla.org/"</span><span class="punctuation token">}</span><span class="punctuation token">,</span>
  logCookie
<span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<p>In the second case, <code>lastError</code> will be passed into the second argument of <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then">Promise.then()</a></code>:</p>

<pre class="brush: js line-numbers language-js">
<code class="language-js"><span class="keyword token">function</span> <span class="function token">logCookie</span><span class="punctuation token">(</span>c<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>c<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">function</span> <span class="function token">logError</span><span class="punctuation token">(</span>e<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  console<span class="punctuation token">.</span><span class="function token">error</span><span class="punctuation token">(</span>e<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>

<span class="keyword token">var</span> setCookie <span class="operator token">=</span> browser<span class="punctuation token">.</span>cookies<span class="punctuation token">.</span><span class="keyword token">set</span><span class="punctuation token">(</span>
  <span class="punctuation token">{</span>url<span class="punctuation token">:</span> <span class="string token">"https://developer.mozilla.org/"</span><span class="punctuation token">}</span>
<span class="punctuation token">)</span><span class="punctuation token">;
</span>
setCookie<span class="punctuation token">.</span><span class="function token">then</span><span class="punctuation token">(</span>logCookie<span class="punctuation token">,</span> logError<span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<div class="note">
<p>Note: <code>runtime.lastError</code> is an alias for {{WebExtAPIRef("extension.lastError")}}: they are set together, and checking either one will work.</p>
</div>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{WebExtBrowserCompat}}</p>

<p>{{WebExtExamples}}</p>

<div class="note"><strong>Acknowledgements</strong>

<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/runtime#property-lastError"><code>chrome.runtime</code></a> API. This documentation is derived from <a href="https://chromium.googlesource.com/chromium/src/+/master/extensions/common/api/runtime.json"><code>runtime.json</code></a> in the Chromium code.</p>

<p>Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.</p>
</div>

<div class="hidden">
<pre>
// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>
</div>
Revert to this revision