How to use the @tanker/errors.NetworkError function in @tanker/errors

To help you get started, we’ve selected a few @tanker/errors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TankerHQ / sdk-js / packages / stream / cloud-storage / src / s3 / DownloadStream.js View on Github external
}, {
        retries: 2,
      });

      const { status, headers } = response;
      const body = await response.arrayBuffer();

      result = new Uint8Array(body);

      if (!this._totalLength) {
        // Partial content: file is incomplete (i.e. bigger than chunkSize)
        if (status === 206) {
          const header = headers.get('content-range'); // e.g. "bytes 786432-1048575/1048698"

          if (typeof header !== 'string' || !header.match(/^bytes +\d+-\d+\/\d+$/)) {
            throw new NetworkError(`S3 answered with status 206 but an invalid content-range header: ${header}`);
          }

          this._totalLength = parseInt(header.split('/')[1], 0);
        } else {
          this._totalLength = result.length;
        }

        this.log(`found total length ${this._totalLength}`);
      }

      this.log(`received chunk of size ${result.length} and status ${status}`);
      this._downloadedLength += result.length;
    } catch (e) {
      this.emit('error', e);
      return;
    }
github TankerHQ / sdk-js / packages / stream / cloud-storage / src / gcs / DownloadStream.js View on Github external
}, {
        retries: 2,
      });

      const { status, headers } = response;
      const body = await response.arrayBuffer();

      result = new Uint8Array(body);

      if (!this._totalLength) {
        // Partial content: file is incomplete (i.e. bigger than chunkSize)
        if (status === 206) {
          const header = headers.get('content-range'); // e.g. "bytes 786432-1048575/1048698"

          if (typeof header !== 'string' || !header.match(/^bytes +\d+-\d+\/\d+$/)) {
            throw new NetworkError(`GCS answered with status 206 but an invalid content-range header: ${header}`);
          }

          this._totalLength = parseInt(header.split('/')[1], 0);
        } else {
          this._totalLength = result.length;
        }

        this.log(`found total length ${this._totalLength}`);
      }

      this.log(`received chunk of size ${result.length} and status ${status}`);
      this._downloadedLength += result.length;
    } catch (e) {
      this.emit('error', e);
      return;
    }
github TankerHQ / sdk-js / packages / stream / cloud-storage / src / s3 / DownloadStream.js View on Github external
async getMetadata(): Promise<{ metadata: string, encryptedContentLength: number }> {
    const response = await fetch(this._headUrl, { method: 'HEAD' });

    const { ok, status, statusText, headers } = response;
    if (!ok) {
      if (status === 404) {
        throw new InvalidArgument(`Could not find any uploaded file that matches the provided resourceId: ${this._resourceId}`);
      } else {
        throw new NetworkError(`S3 metadata head request failed with status ${status}: ${statusText}`);
      }
    }

    const metadata = headers.get('x-amz-meta-tanker-metadata');
    return { metadata, encryptedContentLength: parseInt(headers.get('content-length'), 10) };
  }
github TankerHQ / sdk-js / packages / core / src / Network / Client.js View on Github external
errorListener = this.registerListener('connect_error', (err) => {
        const error = new NetworkError(`can't connect socket: ${err && err.message} ${err && err.description && err.description.message}`);
        cleanup();
        this.socket.abortRequests(error);
        reject(error);
      });
github TankerHQ / sdk-js / packages / core / src / Network / SocketIoWrapper.js View on Github external
    this.socket.on('session error', reason => this.abortRequests(new NetworkError(`socket disconnected by server: ${reason}`)));
    this.socket.on('disconnect', reason => this.abortRequests(new NetworkError(`socket disconnected: ${reason}`)));
github TankerHQ / sdk-js / packages / stream / cloud-storage / src / gcs / DownloadStream.js View on Github external
async getMetadata(): Promise<{ metadata: string, encryptedContentLength: number }> {
    const response = await fetch(this._url, { method: 'HEAD' });

    const { ok, status, statusText, headers } = response;
    if (!ok) {
      if (status === 404) {
        throw new InvalidArgument(`Could not find any uploaded file that matches the provided resourceId: ${this._resourceId}`);
      } else {
        throw new NetworkError(`GCS metadata head request failed with status ${status}: ${statusText}`);
      }
    }

    const metadata = headers.get('x-goog-meta-tanker-metadata');
    return { metadata, encryptedContentLength: parseInt(headers.get('content-length'), 10) };
  }
github TankerHQ / sdk-js / packages / stream / cloud-storage / src / gcs / UploadStream.js View on Github external
async initialize() {
    this.log('initializing...');

    const { ok, status, statusText, headers } = await fetch(this._initUrl, { method: 'POST', headers: this._headers });
    if (!ok) {
      throw new NetworkError(`GCS init request failed with status ${status}: ${statusText}`);
    }
    this._uploadUrl = headers.get('location');
    this.log(`using upload URL ${this._uploadUrl}`);
  }