Better handle cf sorting errors

This commit is contained in:
shamoon 2024-12-23 15:51:16 -08:00
parent aa6aad1a23
commit 20f4409697
4 changed files with 61 additions and 12 deletions

View File

@ -156,7 +156,7 @@ describe('DocumentListViewService', () => {
expect(documentListViewService.currentPage).toEqual(1) expect(documentListViewService.currentPage).toEqual(1)
}) })
it('should handle error on filtering request', () => { it('should handle object error on filtering request', () => {
documentListViewService.currentPage = 1 documentListViewService.currentPage = 1
const tags__id__in = 'hello' const tags__id__in = 'hello'
const filterRulesAny = [ const filterRulesAny = [
@ -185,6 +185,50 @@ describe('DocumentListViewService', () => {
) )
}) })
it('should handle object error on filtering request for custom field sorts', () => {
documentListViewService.currentPage = 1
documentListViewService.sortField = 'custom_field_999'
let req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-custom_field_999&truncate_content=true`
)
expect(req.request.method).toEqual('GET')
req.flush(
{ custom_field_999: ['Custom field not found'] },
{ status: 400, statusText: 'Unexpected error' }
)
expect(documentListViewService.error).toEqual(
'custom_field_999: Custom field not found'
)
// reset the list
documentListViewService.sortField = 'created'
req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true`
)
})
it('should handle string error on filtering request', () => {
documentListViewService.currentPage = 1
const tags__id__in = 'hello'
const filterRulesAny = [
{
rule_type: FILTER_HAS_TAGS_ANY,
value: tags__id__in,
},
]
documentListViewService.filterRules = filterRulesAny
let req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true&tags__id__in=${tags__id__in}`
)
expect(req.request.method).toEqual('GET')
req.flush('Generic error', { status: 404, statusText: 'Unexpected error' })
expect(documentListViewService.error).toEqual('Generic error')
// reset the list
documentListViewService.filterRules = []
req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true`
)
})
it('should support setting sort', () => { it('should support setting sort', () => {
expect(documentListViewService.sortField).toEqual('created') expect(documentListViewService.sortField).toEqual('created')
expect(documentListViewService.sortReverse).toBeTruthy() expect(documentListViewService.sortReverse).toBeTruthy()

View File

@ -307,18 +307,23 @@ export class DocumentListViewService {
activeListViewState.currentPage = 1 activeListViewState.currentPage = 1
this.reload() this.reload()
} else { } else {
console.log(error)
this.selectionData = null this.selectionData = null
let errorMessage let errorMessage
if ( if (
typeof error.error !== 'string' && typeof error.error === 'object' &&
Object.keys(error.error).length > 0 Object.keys(error.error).length > 0
) { ) {
// e.g. { archive_serial_number: Array<string> } // e.g. { archive_serial_number: Array<string> }
errorMessage = Object.keys(error.error) errorMessage = Object.keys(error.error)
.map((fieldName) => { .map((fieldName) => {
const fieldNameBase = fieldName.split('__')[0]
const fieldError: Array<string> = error.error[fieldName] const fieldError: Array<string> = error.error[fieldName]
return `${ return `${
this.sortFields.find((f) => f.field == fieldName)?.name this.sortFields.find(
(f) => f.field?.split('__')[0] == fieldNameBase
)?.name ?? fieldNameBase
}: ${fieldError[0]}` }: ${fieldError[0]}`
}) })
.join(', ') .join(', ')

View File

@ -781,7 +781,9 @@ class DocumentsOrderingFilter(OrderingFilter):
try: try:
field = CustomField.objects.get(pk=custom_field_id) field = CustomField.objects.get(pk=custom_field_id)
except CustomField.DoesNotExist: except CustomField.DoesNotExist:
raise ValueError("Custom field not found") raise serializers.ValidationError(
{self.prefix + str(custom_field_id): [_("Custom field not found")]},
)
annotation = None annotation = None
match field.data_type: match field.data_type:

View File

@ -2920,11 +2920,10 @@ class TestDocumentApiCustomFieldsSorting(DirectoriesMixin, APITestCase):
- 400 is returned - 400 is returned
""" """
with self.assertRaises(ValueError): response = self.client.get(
response = self.client.get( "/api/documents/?ordering=custom_field_999",
"/api/documents/?ordering=custom_field_999", )
) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_document_custom_fields_sorting_invalid_data_type(self): def test_document_custom_fields_sorting_invalid_data_type(self):
""" """
@ -2933,7 +2932,7 @@ class TestDocumentApiCustomFieldsSorting(DirectoriesMixin, APITestCase):
WHEN: WHEN:
- API request for document filtering with a custom field sorting with a new (unhandled) data type - API request for document filtering with a custom field sorting with a new (unhandled) data type
THEN: THEN:
- 400 is returned - Error is raised
""" """
custom_field = CustomField.objects.create( custom_field = CustomField.objects.create(
@ -2942,7 +2941,6 @@ class TestDocumentApiCustomFieldsSorting(DirectoriesMixin, APITestCase):
) )
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
response = self.client.get( self.client.get(
f"/api/documents/?ordering=custom_field_{custom_field.pk}", f"/api/documents/?ordering=custom_field_{custom_field.pk}",
) )
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)