Skip to content

Service_.prototype.hasAccess doesn't set this.lastError_ if canGetToken is false #543

@jonnybolton16

Description

@jonnybolton16

After attempting to obtain an access token using service.getAccessToken(), I am accessing service.lastError_ in the catch block. However, this is undefined. Having looked at the source code, I see service.lastError_ is only assigned if canGetToken is true.

I believe in this case canGetToken is false because I haven't authenticated yet. Actually, I have, but my userProperties got cleared which has lost the stored token information; I need my catch block to re-run the authentication.

Is it possible for the source code to be amended, so that service.lastError_ gets assigned if canGetToken is false?

For context, this is my current code

function getAccessToken_() {
  try {
    return monzoService.getAccessToken();
  } catch {
    const msg = monzoService.lastError_.message;
    if (msg.includes('Client not found')) {
      ...obtain correct Client ID from user...
      monzoService = getMonzoService_();
      return getAccessToken_()
    } else if (msg.includes('Could not authenticate with provided credentials')) {
      ...obtain correct Client secret from user...
      monzoService = getMonzoService_();
      return getAccessToken_()
    } else if (msg.includes('Bad refresh token')) {
      reauthorise_();
    } else {
      throw new Error(msg);
    }
  }
}

Ideally msg will have some flag that will tell me I need to run reauthorise_, when canGetToken is false.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions