/**
 * Copyright 2018-present Facebook.
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 * @format
 */

import {getInstance} from '../fb-stubs/Logger';

/*
 * Wraps a Promise, preserving it's functionality but logging the success or
 failure state of it, with a given name, based on whether it's fulfilled or
 rejected.

 Use this variant to report failures in core platform (Flipper) code.
 */
export function reportPlatformFailures<T>(
  promise: Promise<T>,
  name: string,
): Promise<T> {
  return promise.then(
    fulfilledValue => {
      getInstance().track('success-rate', name, 1);
      return fulfilledValue;
    },
    rejectionReason => {
      getInstance().track('success-rate', name, 0);
      return Promise.reject(rejectionReason);
    },
  );
}

/*
 * Wraps a Promise, preserving it's functionality but logging the success or
 failure state of it, with a given name, based on whether it's fulfilled or
 rejected.

 Use this variant to report failures in plugin code.
 */
export function reportPluginFailures<T>(
  promise: Promise<T>,
  name: string,
  plugin: string,
): Promise<T> {
  return promise.then(
    fulfilledValue => {
      getInstance().track('success-rate', name, 1, plugin);
      return fulfilledValue;
    },
    rejectionReason => {
      getInstance().track('success-rate', name, 0, plugin);
      return Promise.reject(rejectionReason);
    },
  );
}

/*
 * Wraps a closure, preserving it's functionality but logging the success or
 failure state of it.
 */
export function tryCatchReportPlatformFailures<T>(
  closure: () => T,
  name: string,
): T {
  try {
    const result = closure();
    getInstance().track('success-rate', name, 1);
    return result;
  } catch (e) {
    getInstance().track('success-rate', name, 0);
    throw e;
  }
}

@@ -621,51 +621,54 @@
     
-getInstance().track('s
+logPlatformS
 uccess
--rate', 
+Rate(
 name, 
-1
+%7BisSuccess: true%7D
 );%0A 
@@ -737,51 +737,79 @@
     
-getInstance().track('success-rate', name, 0
+logPlatformSuccessRate(name, %7BisSuccess: false, error: rejectionReason%7D
 );%0A 
@@ -1264,59 +1264,60 @@
     
-getInstance().track('s
+logPluginS
 uccess
--rate', 
+Rate(
 name,
- 1,
  plugin
+, %7BisSuccess: true%7D
 );%0A 
@@ -1386,59 +1386,110 @@
     
-getInstance().track('s
+logPluginS
 uccess
--rate', 
+Rate(
 name,
- 0,
  plugin
+, %7B%0A        isSuccess: false,%0A        error: rejectionReason,%0A      %7D
 );%0A 
@@ -1794,86 +1794,704 @@
     
-getInstance().track('success-rate', name, 1);%0A    return result;%0A  %7D catch (e)
+logPlatformSuccessRate(name, %7BisSuccess: true%7D);%0A    return result;%0A  %7D catch (e) %7B%0A    logPlatformSuccessRate(name, %7BisSuccess: false, error: e%7D);%0A    throw e;%0A  %7D%0A%7D%0A%0Atype Result = %7BisSuccess: true%7D %7C %7BisSuccess: false, error: any%7D;%0A%0Afunction logPlatformSuccessRate(name: string, result: Result) %7B%0A  if (result.isSuccess) %7B%0A    getInstance().track('success-rate', name, %7Bvalue: 1%7D);%0A  %7D else %7B%0A    getInstance().track('success-rate', name, %7B%0A      value: 0,%0A      error: extractMessage(result.error),%0A    %7D);%0A  %7D%0A%7D%0A%0Afunction logPluginSuccessRate(name: string, plugin: string, result: Result) %7B%0A  if (result.isSuccess) %7B%0A    getInstance().track('success-rate', name, %7Bvalue: 1%7D, plugin);%0A  %7D else
  %7B%0A 
@@ -2505,32 +2505,39 @@
 nstance().track(
+%0A      
 'success-rate', 
@@ -2539,34 +2539,234 @@
 te',
- name, 0);%0A    throw e;%0A  %7D
+%0A      name,%0A      %7Bvalue: 1, error: extractMessage(result.error)%7D,%0A      plugin,%0A    );%0A  %7D%0A%7D%0A%0Afunction extractMessage(error: any) %7B%0A  if (error instanceof Error) %7B%0A    return error.message;%0A  %7D%0A  return JSON.stringify(error);
 %0A%7D%0A

Include error messages in success-rate metrics