Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Error is lost on failed requests in a batch #35

Open
5 of 6 tasks
jasonjoh opened this issue Apr 22, 2020 · 4 comments
Open
5 of 6 tasks

Error is lost on failed requests in a batch #35

jasonjoh opened this issue Apr 22, 2020 · 4 comments
Assignees

Comments

@jasonjoh
Copy link
Member

Bug Report

Prerequisites

  • Can you reproduce the problem?
  • Are you running the latest version?
  • Are you reporting to the correct repository?
  • Did you perform a cursory search?

Description

When one or more requests in a batch fail, you cannot access the error from the body of the response.

Console Errors: [Is there any console error]

Screenshots: [If applicable, add screenshots to help explain your problem]

Steps to Reproduce

  1. Authenticate in your app with only User.Read.
  2. Run code:
// Create a batch request step to GET /me
  let userRequestStep: MicrosoftGraph.BatchRequestStep = {
    id: "1",
    request: new Request("/me", {
      method: "GET"
    })
  }

  let today = moment({hour: 0, minute: 0, seconds: 0});

  // Create a batch request step to add an event
  let newEvent: Event = {
    subject: "File end-of-day report",
    start: {
      // 5:00 PM
      dateTime: today.add(17, "hours")
        .format("YYYY-MM-DDTHH:mm:ss"),
      timeZone: moment.tz.guess()
    },
    end: {
      // 5:30 PM
      dateTime: today.add(17, "hours").add(30, "minutes")
        .format("YYYY-MM-DDTHH:mm:ss"),
      timeZone: moment.tz.guess()
    }
  }

  let addEventRequestStep: MicrosoftGraph.BatchRequestStep = {
    id: "2",
    // This step will happen after step 1
    dependsOn: [ "1" ],
    request: new Request("/me/events", {
      method: "POST",
      body: JSON.stringify(newEvent),
      headers: {
        "Content-Type": "application/json"
      }
    })
  }

// Create the batch request content with the steps created
  // above
  let batchRequestContent = new MicrosoftGraph.BatchRequestContent([
    userRequestStep,
    addEventRequestStep
  ]);

  let content = await batchRequestContent.getContent();

  // POST the batch request content to the /$batch endpoint
  let batchResponse = await client
    .api('/$batch')
    .post(content);

// Get the create event response by id
  let newEventResponse = batchResponseContent.getResponseById("2");
  if (!newEventResponse.ok) {
    let error: any = await newEventResponse.json();
  }
  1. Second request will fail with a 403 since the app does not have the Calendars.ReadWrite scope.

Expected behavior: The error object in the body of the response (verified by Fiddler) should be returned.

Actual behavior: An error is logged to the console:

SyntaxError: Unexpected token o in JSON at position 1
    at Object.doDependentBatchRequest [as selectedSample] (GraphService.ts:295)
    at async Samples.onRunSample (Samples.tsx:49)

await newEventResponse.text() returns [object Object].

Additional Context

I believe that the fix in microsoftgraph/msgraph-sdk-javascript#204 was incomplete. It only JSON-ifies the body if there's a Content-Type header, and in these error responses, it's absent.

Relevant response from Fiddler trace:

{
  "id": "2",
  "status": 403,
  "headers": {
    "Cache-Control": "private"
  },
  "body": {
    "error": {
      "code": "ErrorAccessDenied",
      "message": "Access is denied. Check credentials and try again.",
      "innerError": {
        "request-id": "1c6101f2-39cf-4a86-9c8c-705c62c08eb5",
        "date": "2020-04-22T18:50:11"
      }
    }
  }
}

Usage Information

SDK Version - 2.0.0

  • Node (Check, if using Node version of SDK)

Node Version - [The version of Node you are using]

  • Browser (Check, if using Browser version of SDK)

Browser Name - Chrome, Edge (Chromium)

Version - 81.0.4044.113, 83.0.478.10

@MIchaelMainer
Copy link

AGS should be returning Content-Type on batch responses when there is one or more error responses in the batch. We need to follow up with AGS on this. @sriram-d Are you aware of this issue?

@ddyett
Copy link

ddyett commented Sep 14, 2020

we should follow up with the service team here as this impacts our experience.

@sriram-d
Copy link

This is a bug in the service. Fix is ready, but rollout will happen post thanksgiving lockdown.

@nikithauc nikithauc transferred this issue from microsoftgraph/msgraph-sdk-javascript Nov 19, 2020
@nikithauc nikithauc assigned sriram-d and unassigned nikithauc Nov 19, 2020
@petrhollayms
Copy link
Collaborator

Thank you for reporting this issue. This appears to be an issue or limitation with the service APIs. Unfortunately, as the Microsoft Graph SDK team, we do not have ownership of the APIs that are causing you issues. We invite you to create a question about the service API to Microsoft Q&A and tagged with one of the [microsoft-graph-*] tags, that way it will get routed to the appropriate team for them to triage:

https://aka.ms/msgraphsupport or directly https://aka.ms/askgraph

For now, we will close the issue on our side but feel free to open it in the relevant repository if you think the issue is specific to SDK.
Please let us know if this helps!

Note: We will close this repository on April 19, 2024.

@petrhollayms petrhollayms assigned jasonjoh and unassigned sriram-d Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants