<commit_before>import funtch, { errorHandler } from 'funtch';
import btoa from '../Tools/btoa';
import localStorageService from './LocalStorageService';

const API_HOST = process.env.API_HOST || 'localhost';
const API = `https://${API_HOST}/`;
const WS = `wss://${API_HOST}/ws/`;

/**
 * Storage key name for authentification token.
 */
export const authStorage = 'auth';

function toStringErr(response) {
  return new Promise((resolve, reject) => {
    errorHandler(response).catch(err =>
      reject({
        ...err,
        toString: () => {
          if (typeof err.content === 'string') {
            return err.content;
          }
          return JSON.stringify(err.content);
        },
      }),
    );
  });
}

/**
 * Generate FetchBuilder for given URL with auth and error handler.
 * @param  {String} url   Wanted URL
 * @return {FetchBuilder} FetchBuilder pre-configured
 */
function auth(url) {
  return funtch.url(url).auth(localStorageService.getItem(authStorage)).error(toStringErr);
}

/**
 * Docker API Service.
 */
export default class DockerService {
  /**
   * Check if User is already logged.
   * @return {Boolean} True if User has authentification token, false  otherwise.
   */
  static isLogged() {
    return !!localStorageService.getItem(authStorage);
  }

  /**
   * Login User given username and password. If success, store auth token in LocalStorage.
   * @param  {String} username User's username
   * @param  {String} password <PASSWORD>
   * @return {Promise}         Token wrapped in a Promise
   */
  static login(username, password) {
    const hash = `Basic ${btoa(`${username}:${password}`)}`;

    return funtch.url(`${API}auth`).auth(hash).get().then((result) => {
      localStorageService.setItem(authStorage, hash);<commit_msg>Fixing error with custom error handler<commit_after>import funtch, { errorHandler } from 'funtch';
import btoa from '../Tools/btoa';
import localStorageService from './LocalStorageService';

const API_HOST = process.env.API_HOST || 'localhost';
const API = `https://${API_HOST}/`;
const WS = `wss://${API_HOST}/ws/`;

/**
 * Storage key name for authentification token.
 */
export const authStorage = 'auth';

function customError(response) {
  return new Promise((resolve, reject) => {
    const funtchResponse = errorHandler(response);
    if (funtchResponse instanceof Promise) {
      return funtchResponse.catch(err =>
        reject({
          ...err,
          toString: () => {
            if (typeof err.content === 'string') {
              return err.content;
            }
            return JSON.stringify(err.content);
          },
        }),
      );
    }
    return resolve(funtchResponse);
  });
}

/**
 * Generate FetchBuilder for given URL with auth and error handler.
 * @param  {String} url   Wanted URL
 * @return {FetchBuilder} FetchBuilder pre-configured
 */
function auth(url) {
  return funtch.url(url).auth(localStorageService.getItem(authStorage)).error(customError);
}

/**
 * Docker API Service.
 */
export default class DockerService {
  /**
   * Check if User is already logged.
   * @return {Boolean} True if User has authentification token, false  otherwise.
   */
  static isLogged() {
    return !!localStorageService.getItem(authStorage);
  }

  /**
   * Login User given username and password. If success, store auth token in LocalStorage.
   * @param  {String} username User's username
   * @param  {String} password User's password
   * @return {Promise}         Token wrapped in a Promise
   */
  static login(username, password) {
    const hash = `Basic ${btoa(`${username}:${password}`)}`;

    return funtch.url(`${API}auth`).auth(hash).get().then((result) => {
      localStorageService.setItem(authStorage, hash);
{'fraction_non_alphanumeric': 0.13861386138613863, 'fraction_numerical': 0.0, 'mean_word_length': 3.2, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2946452', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1093, 'n_words': 297}