Skip to content

Commit

Permalink
add: enhance error message with feed owner and feed resurce access
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Oct 11, 2024
1 parent daeac9a commit 3988553
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
3 changes: 3 additions & 0 deletions public/locales/gsa-de.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"About GSA": "Über GSA",
"Access Complexity": "Zugangskomplexität",
"Access Vector": "Zugangsvektor",
"Access to the feed resources is currently restricted.": "Der Zugriff auf die Feed-Ressourcen ist derzeit eingeschränkt.",
"Actions": "Aktionen",
"Activate the \"attach\" option to allow changes here.": "Aktivieren Sie sie \"Anhängen\"-Option, um hier Änderungen vorzunehmen.",
"Activate the \"include\" option to make changes here.": "Aktivieren Sie die \"Einfügen\"-Option, um hier Änderungen vorzunehmen.",
Expand Down Expand Up @@ -1688,6 +1689,7 @@
"The families selection is STATIC. New families will NOT automatically be added and considered.": "Die Familien-Auswahl ist STATISCH. Neue Familien werden NICHT automatisch hinzugefügt und berücksichtigt.",
"The family selection is DYNAMIC. New families will automatically be added and considered.": "Die Familien-Auswahl ist DYNAMISCH. Neue Familien werden automatisch hinzugefügt und berücksichtigt.",
"The family selection is STATIC. New families will NOT automatically be added and considered.": "Die Familien-Auswahl ist STATISCH. Neue Familien werden NICHT automatisch hinzugefügt und berücksichtigt.",
"The feed owner is currently not set.": "Der Feed-Besitzer ist derzeit nicht festgelegt.",
"The following filter is currently applied: ": "Angewandter Filter: ",
"The last": "Jeden letzten",
"The last {{weekday}} every month": "Jeden letzten {{weekday}} jeden Monat",
Expand Down Expand Up @@ -1719,6 +1721,7 @@
"This form received invalid values. Please check the inputs and submit again.": "Dieses Formular erhielt ungültige Werte. Bitte überprüfen Sie Ihre Eingaben und senden sie erneut ab.",
"This is an Alterable Audit. Reports may not relate to current Policy or Target!": "Dies ist ein änderbares Audit. Berichte könnten sich nicht auf die aktuelle Richtlinie oder das aktuelle Ziel beziehen!",
"This is an Alterable Task. Reports may not relate to current Scan Config or Target!": "Dies ist eine änderbare Aufgabe. Berichte könnten sich nicht auf die aktuelle Scan-Konfiguration oder das aktuelle Ziel beziehen!",
"This issue may be due to the feed not having completed its synchronization.\nPlease try again shortly.": "Dieses Problem könnte daran liegen, dass der Feed seine Synchronisation noch nicht abgeschlossen hat.\nBitte versuchen Sie es in Kürze erneut.",
"This setting is not alterable once task has been run at least once.": "Diese Einstellung ist nicht änderbar sobald die Aufgabe mindestens einmal ausgeführt wurde.",
"This setting is not alterable once the audit has been run at least once.": "Diese Einstellung ist nicht änderbar sobald das audit mindestens einmal ausgeführt wurde.",
"This web application uses cookies to store session information. The cookies are not stored on the server side hard disk and not submitted anywhere. They are lost when the session is closed or expired. The cookies are stored temporarily in your browser as well where you can examine the content.": "Diese Web-Anwendung nutzt Cookies, um Sitzungsinformationen zu speichern. Die Cookies werden nicht auf der serverseitigen Festplatte gespeichert und nirgendwohin übermittelt. Sie gehen verloren, wenn die Sitzung beendet wird oder ausläuft. Die Cookies werden außerdem temporär in Ihrem Browser gespeichert, wo Sie den Inhalt einsehen können.",
Expand Down
36 changes: 36 additions & 0 deletions src/gmp/commands/feedstatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@ export class FeedStatus extends HttpCommand {
throw error;
}
}

/**
* Checks if the current user is the owner of the feed and if they have access to feed resources.
*
* @async
* @function checkFeedOwnerAndPermissions
* @returns {Promise<Object>} An object containing two boolean properties:
* - `isFeedOwner`: Indicates if the user is the owner of the feed.
* - `isFeedResourcesAccess`: Indicates if the user has access to feed resources.
* @throws Will throw an error if the HTTP request fails.
*/
async checkFeedOwnerAndPermissions() {
try {
const {
data: {
get_feeds: {
get_feeds_response: {
feed_owner_set: feedOwner,
feed_resources_access: feedResourcesAccess,
},
},
},
} = await this.httpGet();

const feedOwnerBoolean = Boolean(feedOwner);
const feedResourcesAccessBoolean = Boolean(feedResourcesAccess);

return {
isFeedOwner: feedOwnerBoolean,
isFeedResourcesAccess: feedResourcesAccessBoolean,
};
} catch (error) {
console.error('Error checking feed owner and permissions:', error);
throw error;
}
}
}

registerCommand('feedstatus', FeedStatus);
Expand Down
55 changes: 43 additions & 12 deletions src/gmp/http/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Response from './response';
import DefaultTransform from './transform/default';

import {buildUrlParams} from './utils';
import {FeedStatus} from 'gmp/commands/feedstatus';

const log = logger.getLogger('gmp.http');

Expand All @@ -24,6 +25,23 @@ function formdata_append(formdata, key, value) {
}
}

async function checkFeedOwnershipAndAccess(context) {
const feedStatus = new FeedStatus(context);
const {isFeedOwner, isFeedResourcesAccess} =
await feedStatus.checkFeedOwnerAndPermissions();
const syncMessage = _(
'This issue may be due to the feed not having completed its synchronization.\nPlease try again shortly.',
);

if (!isFeedOwner) {
return `${_('The feed owner is currently not set.')} ${syncMessage}`;
} else if (!isFeedResourcesAccess) {
return `${_('Access to the feed resources is currently restricted.')} ${syncMessage}`;
}

return '';
}

class Http {
constructor(url, options = {}) {
const {timeout, transform = DefaultTransform} = options;
Expand Down Expand Up @@ -157,26 +175,39 @@ class Http {
}
}

handleResponseError(resolve, reject, xhr, options) {
let promise = Promise.reject(xhr);
async handleResponseError(_resolve, reject, xhr, options) {
try {
let request = xhr;

for (const interceptor of this.errorHandlers) {
promise = promise.catch(interceptor);
}
for (const interceptor of this.errorHandlers) {
try {
await interceptor(request);
} catch (err) {
request = err;
}
}

promise.catch(request => {
const {status} = request;
const rej = new Rejection(
request,
status === 401 ? Rejection.REASON_UNAUTHORIZED : Rejection.REASON_ERROR,
);
try {
reject(this.transformRejection(rej, options));
} catch (error) {
log.error('Could not transform rejection', error, rej);
reject(rej);

let rejectedResponse = await this.transformRejection(rej, options);

if (rej.status === 404) {
const additionalMessage = await checkFeedOwnershipAndAccess(this);

if (additionalMessage) {
rejectedResponse.message = `${rejectedResponse.message}\n${additionalMessage}`;
}
}
});

reject(rejectedResponse);
} catch (error) {
log.error('Could not handle response error', error);
reject(error);
}
}

handleRequestError(resolve, reject, xhr, options) {
Expand Down
8 changes: 7 additions & 1 deletion src/web/components/dialog/error.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ const DialogError = ({error, onCloseClick}) => {
}
return (
<StyledLayout align={['space-between', 'center']}>
<span>{error}</span>
<span
style={{
whiteSpace: 'pre-line',
}}
>
{error}
</span>
<DialogCloseButton onClick={onCloseClick} title={_('Close')} />
</StyledLayout>
);
Expand Down

0 comments on commit 3988553

Please sign in to comment.