mirror of
https://github.com/LukeHagar/developer.sailpoint.com.git
synced 2025-12-09 12:27:47 +00:00
4.0 KiB
4.0 KiB
id, title, pagination_label, sidebar_label, sidebar_position, sidebar_class_name, keywords, description, slug, tags
| id | title | pagination_label | sidebar_label | sidebar_position | sidebar_class_name | keywords | description | slug | tags | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| udk-error-handling | Error Handling | UDK | Error Handling | 3 | rudk |
|
Error handling in your custom user interfaces | /tools/ui-development-kit/error-handling |
|
If any of your backend calls result in a server error or bad request you'll want to handle those errors.
400 bad request
If you provide an invalid filter or sorter, the list accounts endpoint will return a 400 error. This example awaits the response and does not exit the program when a 4xx level status is received. If a 4xx level status is received then the user will be redirected to an error page.
import {createConfiguration} from '$lib/sailpoint/sdk.js';
import {getFilters, getLimit, getPage, getSorters} from '$lib/Utils.js';
import {error} from '@sveltejs/kit';
import {AccountsApi} from 'sailpoint-api-client';
export const load = async ({url, locals}) => {
const config = createConfiguration(
locals.session!.baseUrl,
locals.idnSession!.access_token,
);
const api = new AccountsApi(config);
const page = getPage(url);
const limit = getLimit(url);
const sorters = getSorters(url);
const filters = getFilters(url);
const reportResp = await api.listAccounts(
{
count: true,
sorters: sorters,
filters: filters,
limit: Number(limit),
offset: Number(page) * Number(limit),
},
{
validateStatus: function (status) {
return status < 500;
},
},
);
if (reportResp.status !== 200) {
error(400, {
message:
'an error occurred while fetching accounts. Please examine your filters and and sorters and try again.',
context: {params: {page, limit, filters, sorters}},
urls: [
'https://developer.sailpoint.com/idn/api/standard-collection-parameters#filtering-results',
],
errData: reportResp.data,
});
}
const totalCount = reportResp.headers['x-total-count'];
const accountData = reportResp.data;
return {accountData, totalCount, params: {page, limit, sorters, filters}};
};
500 server issues
You can update the code block to handle more than just the 400 level statuses. You can see the highlighted code changes to handle any error response from the API call. You can send back an error to the user with the status, a detailed message, the details about the parameters used that caused the error, and the error response from the API.
import {createConfiguration} from '$lib/sailpoint/sdk.js';
import {getFilters, getLimit, getPage, getSorters} from '$lib/Utils.js';
import {error} from '@sveltejs/kit';
import {AccountsApi} from 'sailpoint-api-client';
export const load = async ({url, locals}) => {
const config = createConfiguration(
locals.session!.baseUrl,
locals.idnSession!.access_token,
);
const api = new AccountsApi(config);
const page = getPage(url);
const limit = getLimit(url);
const sorters = getSorters(url);
const filters = getFilters(url);
const reportResp = await api.listAccounts(
{
count: true,
sorters: sorters,
filters: filters,
limit: Number(limit),
offset: Number(page) * Number(limit),
},
{
validateStatus: function (status) {
// highlight-next-line
return status < 550;
},
},
);
if (reportResp.status !== 200) {
// highlight-next-line
error(reportResp.status, {
message:
'an error occurred while fetching accounts. Please examine your filters and and sorters and try again.',
context: {params: {page, limit, filters, sorters}},
urls: [
'https://developer.sailpoint.com/idn/api/standard-collection-parameters#filtering-results',
],
errData: reportResp.data,
});
}
const totalCount = reportResp.headers['x-total-count'];
const accountData = reportResp.data;
return {accountData, totalCount, params: {page, limit, sorters, filters}};
};