tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

LaunchError.h (2710B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_ipc_LaunchError_h
      8 #define mozilla_ipc_LaunchError_h
      9 
     10 #include "mozilla/StaticString.h"
     11 #include "nsError.h"  // for nsresult
     12 
     13 #if defined(XP_WIN)
     14 #  include <windows.h>
     15 #  include <winerror.h>
     16 #endif
     17 
     18 namespace mozilla::ipc {
     19 
     20 class LaunchError {
     21 public:
     22  // The context of an error code (at the very least, the function that emitted
     23  // it and where it was called) is fundamentally necessary to interpret its
     24  // value. Since we should have that, we're not too fussed about integral
     25  // coercions, so long as we don't lose any bits.
     26  //
     27  // Under POSIX, the usual OS-error type is `int`. Under Windows, the usual
     28  // error type is `HRESULT`, which (despite having bitfield semantics) is
     29  // ultimately a typedef for a flavor of (signed) `long`. For simplicity, and
     30  // in the absence of other constraints, our "error type" container is simply
     31  // the join of these two.
     32  using ErrorType = long;
     33 
     34  // Constructor.
     35  //
     36  // The default of `0` is intended for failure cases where we don't have an
     37  // error code -- usually because the function we've called to determine
     38  // failure has only a boolean return, but also for when we've detected some
     39  // internal failure and there's no appropriate `nsresult` for it.
     40  explicit LaunchError(StaticString aFunction, ErrorType aError = 0)
     41      : mFunction(aFunction), mError(aError) {}
     42 
     43 #ifdef WIN32
     44  // The error code returned by ::GetLastError() is (usually) not an HRESULT.
     45  // This convenience-function maps its return values to the segment of
     46  // HRESULT's namespace reserved for that.
     47  //
     48  // This is not formally necessary -- the error location should suffice to
     49  // disambiguate -- but it's nice to have it converted when reading through
     50  // error-values: `0x80070005` is marginally more human-meaningful than `5`.
     51  static LaunchError FromWin32Error(StaticString aFunction, DWORD aError) {
     52    return LaunchError(aFunction, HRESULT_FROM_WIN32(aError));
     53  }
     54 #endif
     55 
     56  // By design, `nsresult` does not implicitly coerce to an integer.
     57  LaunchError(StaticString aFunction, nsresult aError)
     58      : mFunction(aFunction), mError((ErrorType)aError) {}
     59 
     60  StaticString FunctionName() const { return mFunction; }
     61  ErrorType ErrorCode() const { return mError; }
     62 
     63 private:
     64  StaticString mFunction;
     65  ErrorType mError;
     66 };
     67 
     68 }  // namespace mozilla::ipc
     69 
     70 #endif  // ifndef mozilla_ipc_LaunchError_h