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

Support errors in the RFC 7807-compliant format #564

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions js-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@
QUnit.test("json with string prop", testCase("application/json", JSON.stringify({ any: SAMPLE_MESSAGE }), SAMPLE_MESSAGE));
QUnit.test("other json used verbatim", testCase("application/json", "[{}]", "[{}]"));
QUnit.test("other mime", testCase("unknown/unknown", "any", "Bad Request"));

QUnit.test("RFC 7807 - title", testCase(
"application/json",
JSON.stringify({
type: "https://tools.ietf.org/html/rfc7231#section-6.6.1",
title: SAMPLE_MESSAGE,
detail: "any"
}),
SAMPLE_MESSAGE
));

QUnit.test("RFC 7807 - detail fallback", testCase(
"application/json",
JSON.stringify({
detail: SAMPLE_MESSAGE
}),
SAMPLE_MESSAGE
))

});

QUnit.test("Issue #146", function(assert) {
Expand Down
22 changes: 18 additions & 4 deletions js/dx.aspnet.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@

function getErrorMessageFromXhr(xhr) {
var mime = xhr.getResponseHeader("Content-Type"),
responseText = xhr.responseText;
responseText = xhr.responseText,
candidate;

if(!mime)
return null;
Expand All @@ -378,13 +379,22 @@
if(mime.indexOf("application/json") === 0) {
var jsonObj = safeParseJSON(responseText);

if(typeof jsonObj === "string")
if(isNonEmptyString(jsonObj))
return jsonObj;

if(typeof jsonObj === "object") {
candidate = jsonObj.title;
if(isNonEmptyString(candidate))
return candidate;

candidate = jsonObj.detail;
if(isNonEmptyString(candidate))
return candidate;

for(var key in jsonObj) {
if(typeof jsonObj[key] === "string")
return jsonObj[key];
candidate = jsonObj[key];
if(isNonEmptyString(candidate))
return candidate;
}
}

Expand All @@ -402,6 +412,10 @@
}
}

function isNonEmptyString(value) {
return typeof value === "string" && value.length > 0;
}

return {
createStore: function(options) {
return new CustomStore(createStoreConfig(options));
Expand Down