function syncSafetyNet()

in packages/sqrl/src/function/Instance.ts [112:153]


function syncSafetyNet(name: string, fn, config: SafetyNetConfig) {
  const wrapped = fn;
  const { stateArg, vital } = config;
  if (stateArg) {
    const errorProps: ExecutionErrorProperties = { functionName: name };
    if (vital) {
      errorProps.fatal = true;
    }
    return function (state) {
      try {
        const result = wrapped.apply(null, arguments);
        if (isPromise(result)) {
          state.fatal(`Sync sqrl function returned a promise:: ${name}`);
        }
        return result;
      } catch (err) {
        state.logError(err, errorProps);
        return null;
      }
    };
  } else {
    return function () {
      try {
        const result = wrapped.apply(null, arguments);
        if (isPromise(result)) {
          getGlobalLogger().fatal(
            {},
            `Sync sqrl function returned a promise:: ${name}`
          );
        }
        return result;
      } catch (err) {
        // @NOTE: Any sqrl functions that are /expected/ to error should have a state arg
        getGlobalLogger().fatal(
          { err },
          `Error in sqrl function without state ${name}: ${err.toString()}`
        );
        return null;
      }
    };
  }
}