Skip to content

Commit

Permalink
feat: improve response for not found data
Browse files Browse the repository at this point in the history
- change the status code from 404 to 200 (OK)
- use makeRequest function to treat error from endpoint in the frontend
- show error message in SectionError component

Part of #784
  • Loading branch information
Francisco2002 committed Jan 24, 2025
1 parent 7f075dd commit 2bd510f
Show file tree
Hide file tree
Showing 34 changed files with 142 additions and 109 deletions.
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/buildDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get(self, request, build_id):
records = query.select()
if not records:
return create_error_response(
error_message="Build not found", status_code=HTTPStatus.NOT_FOUND
error_message="Build not found", status_code=HTTPStatus.OK
)

return JsonResponse(records[0])
4 changes: 2 additions & 2 deletions backend/kernelCI_app/views/buildTestsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def get(self, request, build_id):

if not result:
return create_error_response(
error_message="Tests not found for this build",
status_code=HTTPStatus.NOT_FOUND,
error_message="No tests found for this build",
status_code=HTTPStatus.OK,
)

return JsonResponse(list(result), safe=False)
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def post(self, request, hardware_id) -> JsonResponse:

if not commit_history:
return create_error_response(
error_message="Commit history not found", status_code=HTTPStatus.NOT_FOUND
error_message="Commit history not found", status_code=HTTPStatus.OK
)

return JsonResponse({"commit_history_table": commit_history}, safe=False)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/hardwareDetailsSummaryView.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def post(self, request, hardware_id) -> Response:

if len(records) == 0:
return Response(
data={"error": "Hardware not found"}, status=HTTPStatus.NOT_FOUND
data={"error": "Hardware not found"}, status=HTTPStatus.OK
)

is_all_selected = len(self.selected_commits) == 0
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/hardwareDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def post(self, request, hardware_id):

if len(records) == 0:
return Response(
data={"error": "Hardware not found"}, status=HTTPStatus.NOT_FOUND
data={"error": "Hardware not found"}, status=HTTPStatus.OK
)

is_all_selected = len(self.selected_commits) == 0
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/hardwareView.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get(self, request):

if not result["hardware"]:
return create_error_response(
error_message="Hardwares not found", status_code=HTTPStatus.NOT_FOUND
error_message="No hardwares found", status_code=HTTPStatus.OK
)

return JsonResponse(result, safe=False)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/issueDetailsBuildsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get(

if not builds_data:
return create_error_response(
error_message="Builds not found", status_code=HTTPStatus.NOT_FOUND
error_message="No builds found for this issue", status_code=HTTPStatus.OK
)

return JsonResponse(builds_data, safe=False)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/issueDetailsTestsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get(

if not tests_data:
return create_error_response(
error_message="Tests not found", status_code=HTTPStatus.NOT_FOUND
error_message="No tests found for this issue", status_code=HTTPStatus.OK
)

return JsonResponse(tests_data, safe=False)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/issueDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get(

if not issue_data:
return create_error_response(
error_message="Issue not found", status_code=HTTPStatus.NOT_FOUND
error_message="Issue not found", status_code=HTTPStatus.OK
)

return JsonResponse(issue_data)
4 changes: 2 additions & 2 deletions backend/kernelCI_app/views/issuesView.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def get(
if len(test_issues) == 0:
return create_error_response(
error_message="No issues were found for this test",
status_code=HTTPStatus.NOT_FOUND,
status_code=HTTPStatus.OK,
)

return JsonResponse(test_issues, safe=False)
Expand All @@ -97,7 +97,7 @@ def get(
if len(build_issues) == 0:
return create_error_response(
error_message="No issues were found for this build",
status_code=HTTPStatus.NOT_FOUND,
status_code=HTTPStatus.OK,
)

return JsonResponse(build_issues, safe=False)
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/logDownloaderView.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get(self, request):

if not parsed_data['log_files']:
return create_error_response(
error_message="No log files found", status_code=HTTPStatus.NOT_FOUND
error_message="No log files found", status_code=HTTPStatus.OK
)

return JsonResponse(parsed_data)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/testDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get(self, _request, test_id: str | None):

if len(response) == 0:
return create_error_response(
error_message="Test not found", status_code=HTTPStatus.NOT_FOUND
error_message="Test not found", status_code=HTTPStatus.OK
)

return JsonResponse(response, safe=False)
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeCommitsHistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def get(self, request, commit_hash):

if not rows:
return create_error_response(
error_message="History of tree commits not found", status_code=HTTPStatus.NOT_FOUND
error_message="History of tree commits not found", status_code=HTTPStatus.OK
)

self._process_rows(rows)
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeDetailsBootsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get(self, request, commit_hash: str | None):

if len(rows) == 0:
return create_error_response(
error_message="Tree not found", status_code=HTTPStatus.NOT_FOUND
error_message="No boots found for this tree", status_code=HTTPStatus.OK
)

try:
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeDetailsBuildsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get(self, request, commit_hash: str | None):

if len(rows) == 0:
return create_error_response(
error_message="Tree not found", status_code=HTTPStatus.NOT_FOUND
error_message="No builds found for this tree", status_code=HTTPStatus.OK
)

try:
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeDetailsSummaryView.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get(self, request, commit_hash: str | None):

if len(rows) == 0:
return create_error_response(
error_message="Tree not found", status_code=HTTPStatus.NOT_FOUND
error_message="Tree not found", status_code=HTTPStatus.OK
)

self.filters = FilterParams(request)
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeDetailsTestsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get(self, request, commit_hash: str | None):

if len(rows) == 0:
return create_error_response(
error_message="Tree not found", status_code=HTTPStatus.NOT_FOUND
error_message="No tests found for this tree", status_code=HTTPStatus.OK
)

try:
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeDetailsView.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def get(self, request, commit_hash: str | None):

if len(rows) == 0:
return create_error_response(
error_message="Tree not found", status_code=HTTPStatus.NOT_FOUND
error_message="Tree not found", status_code=HTTPStatus.OK
)

self._sanitize_rows(rows)
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeLatest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get(self, request, tree_name: str, branch: str) -> JsonResponse:
if tree_data is None:
return create_error_response(
error_message=tree_not_found_error_message,
status_code=HTTPStatus.NOT_FOUND,
status_code=HTTPStatus.OK,
)

base_url = reverse(
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeView.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def get(self, request):

if not checkouts:
return create_error_response(
error_message="Trees not found", status_code=HTTPStatus.NOT_FOUND
error_message="Trees not found", status_code=HTTPStatus.OK
)

serializer = TreeSerializer(checkouts, many=True)
Expand Down
2 changes: 1 addition & 1 deletion backend/kernelCI_app/views/treeViewFast.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get(self, request):

if not checkouts:
return create_error_response(
error_message="Trees not found", status_code=HTTPStatus.NOT_FOUND
error_message="Trees not found", status_code=HTTPStatus.OK
)

# TODO Use django serializer
Expand Down
19 changes: 12 additions & 7 deletions dashboard/src/api/buildDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import type { TBuildDetails } from '@/types/tree/BuildDetails';

import type { TIssue } from '@/types/general';

import http from './api';
import { RequestData } from './commonRequest';

const fetchTreeDetailData = async (buildId: string): Promise<TBuildDetails> => {
const res = await http.get(`/api/build/${buildId}`);
const fetchBuildDetailsData = async (
buildId: string,
): Promise<TBuildDetails> => {
const res = await RequestData.get<TBuildDetails & { _timestamp: string }>(
`/api/build/${buildId}`,
);

const { _timestamp, ...data } = res.data;
const { _timestamp, ...data } = res;
data.timestamp = _timestamp;
return data;
};
Expand All @@ -20,13 +24,14 @@ export const useBuildDetails = (
): UseQueryResult<TBuildDetails> => {
return useQuery({
queryKey: ['treeData', buildId],
queryFn: () => fetchTreeDetailData(buildId),
queryFn: () => fetchBuildDetailsData(buildId),
});
};

const fetchBuildIssues = async (buildId: string): Promise<TIssue[]> => {
const res = await http.get<TIssue[]>(`/api/build/${buildId}/issues`);
return res.data;
const data = await RequestData.get<TIssue[]>(`/api/build/${buildId}/issues`);

return data;
};

export const useBuildIssues = (buildId: string): UseQueryResult<TIssue[]> => {
Expand Down
8 changes: 5 additions & 3 deletions dashboard/src/api/buildTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { useQuery } from '@tanstack/react-query';

import type { TestHistory } from '@/types/general';

import http from './api';
import { RequestData } from './commonRequest';

const fetchBuildTestsData = async (buildId: string): Promise<TestHistory[]> => {
const res = await http.get(`/api/build/${buildId}/tests`);
const data = await RequestData.get<TestHistory[]>(
`/api/build/${buildId}/tests`,
);

return res.data;
return data;
};

export const useBuildTests = (
Expand Down
10 changes: 6 additions & 4 deletions dashboard/src/api/commitHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import type {

import { mapFiltersKeysToBackendCompatible } from '@/utils/utils';

import { getTargetFilter, type TFilter } from '@/types/general';
import { getTargetFilter } from '@/types/general';
import type { TFilter } from '@/types/general';

import http from './api';
import { RequestData } from './commonRequest';

const fetchCommitHistory = async (
commitHash: string,
Expand All @@ -32,13 +33,14 @@ const fetchCommitHistory = async (
...filtersFormatted,
};

const res = await http.get<TTreeCommitHistoryResponse>(
const data = await RequestData.get<TTreeCommitHistoryResponse>(
`/api/tree/${commitHash}/commits`,
{
params,
},
);
return res.data;

return data;
};

export const useCommitHistory = ({
Expand Down
21 changes: 13 additions & 8 deletions dashboard/src/api/hardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import type { HardwareListingResponse } from '@/types/hardware';

import type { TOrigins } from '@/types/general';

import http from './api';
import { RequestData } from './commonRequest';

const fetchHardwareListing = async (
origin: TOrigins,
startTimestampInSeconds: number,
endTimeStampInSeconds: number,
): Promise<HardwareListingResponse> => {
const res = await http.get('/api/hardware/', {
params: {
startTimestampInSeconds,
endTimeStampInSeconds,
origin,
const data = await RequestData.get<HardwareListingResponse>(
'/api/hardware/',
{
params: {
startTimestampInSeconds,
endTimeStampInSeconds,
origin,
},
},
});
return res.data;
);

return data;
};

export const useHardwareListing = (
Expand Down
11 changes: 6 additions & 5 deletions dashboard/src/api/hardwareDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
import type { TFilter, TOrigins } from '@/types/general';
import { getTargetFilter } from '@/types/general';

import http from './api';
import { RequestData } from './commonRequest';

type fetchHardwareDetailsBody = {
startTimestampInSeconds: number;
Expand Down Expand Up @@ -43,12 +43,12 @@ const fetchHardwareDetails = async (
hardwareId: string,
body: fetchHardwareDetailsBody,
): Promise<THardwareDetails> => {
const res = await http.post<THardwareDetails>(
const data = await RequestData.post<THardwareDetails>(
`/api/hardware/${hardwareId}`,
body,
);

return res.data;
return data;
};

const TREE_SELECT_HEAD_VALUE = 'head';
Expand Down Expand Up @@ -115,11 +115,12 @@ const fetchCommitHistory = async (
hardwareId: string,
body: FetchHardwareDetailsCommitHistoryBody,
): Promise<CommitHistoryTable> => {
const res = await http.post<CommitHistoryTable>(
const data = await RequestData.post<CommitHistoryTable>(
`/api/hardware/${hardwareId}/commit-history`,
body,
);
return res.data;

return data;
};

export const useHardwareDetailsCommitHistory = (
Expand Down
Loading

0 comments on commit 2bd510f

Please sign in to comment.