open-consul/ui/packages/consul-ui/app/utils/create-fingerprinter.js
John Cowen 8c8443390d
ui: Improve error messaging for when we can't make a slug (#11697)
Ember Data requires the usage of unique ID to identify its records in the frontend, and we use a centralized function to do that for all records. There are occasions where it can't make an ID, usually this is a bug our side, but there are occasions where Consul might not be giving us the data needed to make an ID, for example if a Service comes down to us with a blank Name. Whilst this isn't a problem to be fixed in the UI, I thought we could make an improvement here by giving a little more info as to why the UI cannot make a unique ID.

This is currently semi-hidden away in the javascript console, but we could potentially surface this in the UI itself as a larger task. I figured this smaller task could help folks in the meantime if they hit upon this as they might open up the javascript console themselves to see whats up and they'd at least get this extra clue.
2021-12-06 16:11:57 +00:00

49 lines
1.7 KiB
JavaScript

import { get } from '@ember/object';
export default function(foreignKey, nspaceKey, partitionKey, hash = JSON.stringify) {
return function(primaryKey, slugKey, foreignKeyValue, nspaceValue, partitionValue) {
return function(item) {
foreignKeyValue = foreignKeyValue == null ? item[foreignKey] : foreignKeyValue;
if (foreignKeyValue == null) {
throw new Error(
`Unable to create fingerprint, missing foreignKey value. Looking for value in \`${foreignKey}\` got \`${foreignKeyValue}\``
);
}
const slugKeys = slugKey.split(',');
const slugValues = slugKeys.map(function(slugKey) {
const slug = get(item, slugKey);
if (slug == null || slug.length < 1) {
throw new Error(
`Unable to create fingerprint, missing slug. Looking for value in \`${slugKey}\` got \`${slug}\``
);
}
return slug;
});
// This ensures that all data objects have a Namespace and a Partition
// value set, even in OSS.
if (typeof item[nspaceKey] === 'undefined') {
if (nspaceValue === '*') {
nspaceValue = 'default';
}
item[nspaceKey] = nspaceValue;
}
if (typeof item[partitionKey] === 'undefined') {
if (partitionValue === '*') {
partitionValue = 'default';
}
item[partitionKey] = partitionValue;
}
if (typeof item[foreignKey] === 'undefined') {
item[foreignKey] = foreignKeyValue;
}
if (typeof item[primaryKey] === 'undefined') {
item[primaryKey] = hash(
[item[partitionKey], item[nspaceKey], foreignKeyValue].concat(slugValues)
);
}
return item;
};
};
}