Skip to content

Commit 65af792

Browse files
Support errors in the RFC 7807-compliant format (#564)
1 parent 9a54925 commit 65af792

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

js-test/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@
174174
QUnit.test("json with string prop", testCase("application/json", JSON.stringify({ any: SAMPLE_MESSAGE }), SAMPLE_MESSAGE));
175175
QUnit.test("other json used verbatim", testCase("application/json", "[{}]", "[{}]"));
176176
QUnit.test("other mime", testCase("unknown/unknown", "any", "Bad Request"));
177+
178+
QUnit.test("RFC 7807 - title", testCase(
179+
"application/json",
180+
JSON.stringify({
181+
type: "https://tools.ietf.org/html/rfc7231#section-6.6.1",
182+
title: SAMPLE_MESSAGE,
183+
detail: "any"
184+
}),
185+
SAMPLE_MESSAGE
186+
));
187+
188+
QUnit.test("RFC 7807 - detail fallback", testCase(
189+
"application/json",
190+
JSON.stringify({
191+
detail: SAMPLE_MESSAGE
192+
}),
193+
SAMPLE_MESSAGE
194+
))
195+
177196
});
178197

179198
QUnit.test("Issue #146", function(assert) {

js/dx.aspnet.data.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@
367367

368368
function getErrorMessageFromXhr(xhr) {
369369
var mime = xhr.getResponseHeader("Content-Type"),
370-
responseText = xhr.responseText;
370+
responseText = xhr.responseText,
371+
candidate;
371372

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

381-
if(typeof jsonObj === "string")
382+
if(isNonEmptyString(jsonObj))
382383
return jsonObj;
383384

384385
if(typeof jsonObj === "object") {
386+
candidate = jsonObj.title;
387+
if(isNonEmptyString(candidate))
388+
return candidate;
389+
390+
candidate = jsonObj.detail;
391+
if(isNonEmptyString(candidate))
392+
return candidate;
393+
385394
for(var key in jsonObj) {
386-
if(typeof jsonObj[key] === "string")
387-
return jsonObj[key];
395+
candidate = jsonObj[key];
396+
if(isNonEmptyString(candidate))
397+
return candidate;
388398
}
389399
}
390400

@@ -402,6 +412,10 @@
402412
}
403413
}
404414

415+
function isNonEmptyString(value) {
416+
return typeof value === "string" && value.length > 0;
417+
}
418+
405419
return {
406420
createStore: function(options) {
407421
return new CustomStore(createStoreConfig(options));

0 commit comments

Comments
 (0)