aminghadersohi commented on code in PR #43191:
URL: https://github.com/apache/superset/pull/43191#discussion_r3806943056


##########
superset-frontend/src/features/users/utils.ts:
##########
@@ -17,10 +17,49 @@
  * under the License.
  */
 import { t } from '@apache-superset/core/translation';
-import { SupersetClient } from '@superset-ui/core';
+import { getClientErrorObject, SupersetClient } from '@superset-ui/core';
 import { SelectOption } from 'src/components/ListView';
+import { Actions } from 'src/constants';
 import { FormValues } from './types';
 
+type AddDangerToast = (message: string) => void;
+
+export const handleUserError = async (
+  err: Response,
+  action: Actions.CREATE | Actions.UPDATE,
+  addDangerToast: AddDangerToast,
+): Promise<never> => {
+  let errorMessage =
+    action === Actions.CREATE
+      ? t('There was an error creating the user. Please, try again.')
+      : t('There was an error updating the user. Please, try again.');
+
+  if (err.status === 400 || err.status === 422) {
+    const errorData = await getClientErrorObject(err);
+    const message: unknown = errorData.message;
+
+    if (err.status === 400 && message && typeof message === 'object') {

Review Comment:
   Good catch — relaxed to `err.status === 400 && message`, plus an 
`errorData.error` truthiness check so a JSON 400 with no usable message still 
falls back to the generic toast. Added a test covering the plain-string shape 
(`User must have at least one role or group!`), and the non-JSON test still 
passes.



##########
superset-frontend/src/features/users/utils.ts:
##########
@@ -17,10 +17,49 @@
  * under the License.
  */
 import { t } from '@apache-superset/core/translation';
-import { SupersetClient } from '@superset-ui/core';
+import { getClientErrorObject, SupersetClient } from '@superset-ui/core';
 import { SelectOption } from 'src/components/ListView';
+import { Actions } from 'src/constants';
 import { FormValues } from './types';
 
+type AddDangerToast = (message: string) => void;
+
+export const handleUserError = async (
+  err: Response,
+  action: Actions.CREATE | Actions.UPDATE,
+  addDangerToast: AddDangerToast,
+): Promise<never> => {
+  let errorMessage =
+    action === Actions.CREATE
+      ? t('There was an error creating the user. Please, try again.')
+      : t('There was an error updating the user. Please, try again.');
+
+  if (err.status === 400 || err.status === 422) {
+    const errorData = await getClientErrorObject(err);
+    const message: unknown = errorData.message;
+
+    if (err.status === 400 && message && typeof message === 'object') {
+      errorMessage = errorData.error;
+    } else if (
+      err.status === 422 &&
+      errorData.error.includes('duplicate key value')

Review Comment:
   Done — `errorData.error?.includes(...)`, so an undefined `error` can no 
longer throw before `addDangerToast`. Added a test for a JSON 422 body with 
neither `message` nor `errors` that asserts the generic toast fires.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to