Microsoft Graph API Error response objects are not consistent and error handling.

Neil Carpenter 1 Reputation point
2022-11-17T10:00:54.507+00:00

I'm building an app with PHP/Laravel, and I'm using the Microsoft Graph API.

Part of this app allows us to create Users via the API, and I've quickly realised that in terms of validating post requests, the error response objects are inconsistent.

For example, if I don't send through a displayName, the error response object looks like this.

   {  
       "code": "Request_BadRequest",  
       "message": "A value is required for property 'displayName' of resource 'User'.",  
       "innerError": {  
           "date": "2022-11-17T09:48:20",  
           "request-id": "***",  
           "client-request-id": "***",  
       },  
   }  

However, when I leave of the mailNickname value off, the error response object looks like this...

   {  
       "code": "Request_BadRequest",  
       "message": "Invalid value specified for property 'mailNickname' of resource 'User'.",  
       "details": [  
           {  
               "code": "InvalidLength"  
               "message": "The mailNickname should be between 1 and 64 characters."  
               "target": "mailNickname"  
           }  
         
       "innerError": {  
           "date": "2022-11-17T09:51:22",  
           "request-id": "***",  
           "client-request-id": "***",  
       }  
   }  

Is there a reason why validation error response objects don't always contain a target or details array?

There doesn't seem to be any resource specific documentation on errors. All I've found is what's on this page of the docs and it doesn't really tell me much.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,743 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RajeshKumarMSFT 1,971 Reputation points Microsoft Vendor
    2022-11-17T13:49:56.18+00:00

    Hi @Neil Carpenter ,

    Hope you are doing well,

    While creating the New users using graph api please make sure we need to fill value for all required properties as per documentation.
    As value for mailNickname is blank you are facing respective error message,which is different when you are not passing the required property in request body(displayName).
    Even you will face the same error message if you dont fill the value for other required fields.(Ex: displayName, accountEnabled ).

    261502-image.png

    Please Refer this links for additional info:-
    https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=http#request-body
    https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=http#response

    Hope this helps.
    If the answer is helpful, please click Accept Answer and kindly Upvote it. If you have any further questions about this answer, please click Comment

    0 comments No comments

  2. Neil Carpenter 1 Reputation point
    2022-11-17T14:10:12.933+00:00

    Okay, based on your response, I understand more about the error response objects I shared, but it doesn't really answer my question.

    So in my examples, when I say I wasn't submitting a displayName - I was actually submitting a NULL value. This is why I get the response I get.

    But with the mailNickname, I was submitting an empty string, and that's why I got the target, and the details, in the error response.... To let me know there's an InvalidLength submitted.

    I understand this now, it makes sense to me, so thank you for that.

    I do, however, think it would make sense to receive details in the error response for value required errors.

    Something like this.

       {  
           "code": "Request_BadRequest",  
           "message": "A value is required for property 'displayName' of resource 'User'.",  
           "details": [  
               {  
                   "code": "RequiredValue",  
                   "message": "The displayName is required.",  
                   "target": "displayName"  
               }  
           ],  
           "innerError": {  
               "date": "2022-11-17T09:51:22",  
               "request-id": "***",  
               "client-request-id": "***",  
           }  
       }  
    

    Is there a reason that we don't get this?

    0 comments No comments

  3. Neil Carpenter 1 Reputation point
    2022-11-17T14:26:24.977+00:00

    I've done a little more digging and I realise that we do receive error responses similar to what I suggested.

    As I touched on in my last reply, the issue I was seeing is all to do with the request body that I was posting.