-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9909 from hashicorp/b-ui/incorrect-error-message
UI: Use server-sent error messages when applicable
- Loading branch information
Showing
8 changed files
with
82 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import { ForbiddenError } from '@ember-data/adapter/error'; | ||
|
||
// Returns a single string based on the response the adapter received | ||
export default function messageFromAdapterError(error) { | ||
if (error.errors) { | ||
export default function messageFromAdapterError(error, actionMessage) { | ||
if (error instanceof ForbiddenError) { | ||
return `Your ACL token does not grant permission to ${actionMessage}.`; | ||
} | ||
|
||
if (error.errors?.length) { | ||
return error.errors.mapBy('detail').join('\n\n'); | ||
} | ||
|
||
return 'Unknown Error'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { module, test } from 'qunit'; | ||
import { ServerError, ForbiddenError } from '@ember-data/adapter/error'; | ||
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error'; | ||
|
||
const testCases = [ | ||
{ | ||
name: 'Forbidden Error', | ||
in: [new ForbiddenError([], "Can't do that"), 'run tests'], | ||
out: 'Your ACL token does not grant permission to run tests.', | ||
}, | ||
{ | ||
name: 'Generic Error', | ||
in: [new ServerError([{ detail: 'DB Max Connections' }], 'Server Error'), 'run tests'], | ||
out: 'DB Max Connections', | ||
}, | ||
{ | ||
name: 'Multiple Errors', | ||
in: [ | ||
new ServerError( | ||
[{ detail: 'DB Max Connections' }, { detail: 'Service timeout' }], | ||
'Server Error' | ||
), | ||
'run tests', | ||
], | ||
out: 'DB Max Connections\n\nService timeout', | ||
}, | ||
{ | ||
name: 'Malformed Error (not from Ember Data which should always have an errors list)', | ||
in: [new Error('Go boom'), 'handle custom error messages'], | ||
out: 'Unknown Error', | ||
}, | ||
]; | ||
|
||
module('Unit | Util | messageFromAdapterError', function() { | ||
testCases.forEach(testCase => { | ||
test(testCase.name, function(assert) { | ||
assert.equal( | ||
messageFromAdapterError.apply(null, testCase.in), | ||
testCase.out, | ||
`[${testCase.in.join(', ')}] => ${testCase.out}` | ||
); | ||
}); | ||
}); | ||
}); |