DriveBackupV2 icon indicating copy to clipboard operation
DriveBackupV2 copied to clipboard

redo graphapierrorexception

Open StillGreen-san opened this issue 2 months ago • 2 comments

this removes wrong GraphApiErrorException parsing logic and improves its fault tolerance

remove incorrect GraphApiError parsing

my previous implementation was wrong. the graph api error docs are a bit wage and i must have missed the link to the more detailed explanation. previously this could result in failure to create the exception. now it only tries to parse the outer most required fields. the complete error object is additionally stored as a JSONObject. this greatly simplifies parsing and printing, which currently are the only use cases for this information anyway.

prevent exceptions during GraphApiErrorException creation

previously if there was an issue parsing the error response, an exception would be thrown, loosing the status code and cluttering the stack trace. now it will always create the exception, simply noting a parsing failure in its message.

remove Response constructor from GraphApiErrorException

Response.body may return null and the exception would need to handle that. the caller however, does most likely have context to determine if it would be null or not. potentially passing a custom json string that would provide more info than a generic 'was null'. it also prevents potential double reads of the body, which are not supported.

use case-insensitive JSON parsing in GraphApiErrorException

the docs use inconsistent capitalization for the json keys. so this change was made to make parsing more tolerant in case this is real world behavior. since case insensitive lookup may be something thats useful elsewhere, it was put in a separat JsonUtil file.

OneDrive cleanup

uploadToSession had become quite large, so i took the opportunity to move accept and error handling into separate functions

print output examples

invalid json:

- org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
+ ratismal.drivebackup.uploaders.onedrive.GraphApiErrorException: 123 invalidErrorResponse : "A JSONObject text must begin with '{' at 1 [character 2 line 1]"
- 	at org.json.JSONTokener.syntaxError(JSONTokener.java:503)
- 	at org.json.JSONObject.<init>(JSONObject.java:213)
- 	at org.json.JSONObject.<init>(JSONObject.java:430)
- 	at ratismal.drivebackup.uploaders.onedrive.GraphApiErrorException.<init>(GraphApiErrorException.java:59)
	at moe.sgs.Main.Try(Main.java:44)
	at moe.sgs.Main.main(Main.java:38)

with inner error:

- ratismal.drivebackup.uploaders.onedrive.GraphApiErrorException: 101 badRequest : "Uploaded fragment overlaps with existing data." inner:[ "invalidRange" ]
+ ratismal.drivebackup.uploaders.onedrive.GraphApiErrorException: 101 badRequest : "Uploaded fragment overlaps with existing data."
+ {
+   "code": "badRequest",
+   "innererror": {
+     "date": "date-time",
+     "code": "invalidRange",
+     "request-id": "request-id"
+   },
+   "message": "Uploaded fragment overlaps with existing data."
+ }
	at moe.sgs.Main.Try(Main.java:44)
	at moe.sgs.Main.main(Main.java:32)

StillGreen-san avatar Nov 18 '25 11:11 StillGreen-san