Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollbar.init() not accept "module" option field when use locals #989

Closed
yamotuki opened this issue Feb 16, 2022 · 2 comments · Fixed by #1004
Closed

Rollbar.init() not accept "module" option field when use locals #989

yamotuki opened this issue Feb 16, 2022 · 2 comments · Fixed by #1004

Comments

@yamotuki
Copy link

yamotuki commented Feb 16, 2022

Problem

I wanted to use "Locals".

I called init function with bellow options.

Rollbar.init({
      accessToken: token,
      captureUncaught: true,
      locals: {
        locals: require('rollbar/src/server/locals'),
        depth: 2,
        uncaughtOnly: false,
        enabled: true
      }
    }
)

Then I get errors.

TypeError 
Locals is not a constructor

node_modules/rollbar/src/server/rollbar.js:71:
initLocals
node_modules/rollbar/src/server/rollbar.js:47:
new Rollbar
node_modules/rollbar/src/server/rollbar.js:79:
Function.module.exports../node_modules/rollbar/src/server/rollbar.js.Rollbar.init
app/plugins/rollbar.ts:22:
module.exports../app/plugins/rollbar.ts.__webpack_exports__.default
.nuxt/index.js:252:
createApp
.nuxt/server.js:97:
module.exports../.nuxt/server.js.__webpack_export

The reason is that options have not "module" field. Because type not accept "module".

index.d.ts descrive as bellow.

    class Locals {}
    export type LocalsType = typeof Locals;
    export type LocalsOptions = LocalsType | LocalsSettings;
    export interface LocalsSettings {
        locals: LocalsType,
        enabled?: boolean;
        uncaughtOnly?: boolean;
        depth?: number;
        maxProperties?: number;
        maxArray?: number;
    }

Workaround

I found that the code with follows options can be excuted.

{
      accessToken: token,
      captureUncaught: true,
      locals: {
        locals: require('rollbar/src/server/locals'),
        depth: 2,
        uncaughtOnly: false,
        enabled: true,
        // @ts-ignore
        module: require('rollbar/src/server/locals')
      }

suggestion for resolution

rollbar/src/server/rollbar.js:65 should be changed.

This is current code.

  var Locals;
  if (typeof localsOptions === 'function') {
    Locals = localsOptions;
    localsOptions = null; // use defaults
  } else if (_.isType(localsOptions, 'object')) {
    Locals = localsOptions.module;
    delete localsOptions.module;
  } else {
    logger.error('options.locals or options.locals.module must be a Locals module');
    return null;
  }
  return new Locals(localsOptions, logger);

suggestion code

  var Locals;
  if (typeof localsOptions === 'function') {
    Locals = localsOptions;
    localsOptions = null; // use defaults
  } else if (_.isType(localsOptions, 'object')) {
    if (localsOptions.module) {
      Locals = localsOptions.module;
      delete localsOptions.module;
    } else if (localsOptions.locals) {
      Locals = localsOptions.locals;
      delete localsOptions.locals;
    }
  } else {
    logger.error('options.locals or options.locals.module must be a Locals module');
    return null;
  }
  return new Locals(localsOptions, logger);

If Needed, I can create pull request.

@waltjones
Copy link
Contributor

This is an issue with the type declaration.

@waltjones
Copy link
Contributor

This is released in 2.25.0. https://github.com/rollbar/rollbar.js/releases/tag/v2.25.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants