This is an automated email from the ASF dual-hosted git repository.

erikrit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 5fb883e  chore(ts): type getClientErrorObject (#9693)
5fb883e is described below

commit 5fb883e279b928fb942bd530e5b3163ecf0cae6a
Author: Erik Ritter <[email protected]>
AuthorDate: Thu Apr 30 17:03:31 2020 -0700

    chore(ts): type getClientErrorObject (#9693)
---
 ...Object_spec.js => getClientErrorObject_spec.ts} |  0
 superset-frontend/src/setup/setupApp.ts            |  8 +-
 .../src/utils/getClientErrorObject.js              | 74 ------------------
 .../src/utils/getClientErrorObject.ts              | 90 ++++++++++++++++++++++
 4 files changed, 95 insertions(+), 77 deletions(-)

diff --git 
a/superset-frontend/spec/javascripts/utils/getClientErrorObject_spec.js 
b/superset-frontend/spec/javascripts/utils/getClientErrorObject_spec.ts
similarity index 100%
rename from 
superset-frontend/spec/javascripts/utils/getClientErrorObject_spec.js
rename to superset-frontend/spec/javascripts/utils/getClientErrorObject_spec.ts
diff --git a/superset-frontend/src/setup/setupApp.ts 
b/superset-frontend/src/setup/setupApp.ts
index e81c6d2..b4a9ca7 100644
--- a/superset-frontend/src/setup/setupApp.ts
+++ b/superset-frontend/src/setup/setupApp.ts
@@ -19,10 +19,12 @@
 /* eslint global-require: 0 */
 import $ from 'jquery';
 import { SupersetClient } from '@superset-ui/connection';
-import getClientErrorObject from '../utils/getClientErrorObject';
+import getClientErrorObject, {
+  ClientErrorObject,
+} from '../utils/getClientErrorObject';
 import setupErrorMessages from './setupErrorMessages';
 
-function showApiMessage(resp: { severity?: string; message: string }) {
+function showApiMessage(resp: ClientErrorObject) {
   const template =
     '<div class="alert"> ' +
     '<button type="button" class="close" ' +
@@ -30,7 +32,7 @@ function showApiMessage(resp: { severity?: string; message: 
string }) {
   const severity = resp.severity || 'info';
   $(template)
     .addClass('alert-' + severity)
-    .append(resp.message)
+    .append(resp.message || '')
     .appendTo($('#alert-container'));
 }
 
diff --git a/superset-frontend/src/utils/getClientErrorObject.js 
b/superset-frontend/src/utils/getClientErrorObject.js
deleted file mode 100644
index fad31a0..0000000
--- a/superset-frontend/src/utils/getClientErrorObject.js
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-import { t } from '@superset-ui/translation';
-import COMMON_ERR_MESSAGES from './errorMessages';
-
-export default function getClientErrorObject(response) {
-  // takes a Response object as input, attempts to read response as Json if 
possible,
-  // and returns a Promise that resolves to a plain object with error key and 
text value.
-  return new Promise(resolve => {
-    if (typeof response === 'string') {
-      resolve({ error: response });
-    } else if (
-      response &&
-      response.constructor === Response &&
-      !response.bodyUsed
-    ) {
-      // attempt to read the body as json, and fallback to text. we must clone 
the
-      // response in order to fallback to .text() because Response is 
single-read
-      response
-        .clone()
-        .json()
-        .then(errorJson => {
-          let error = { ...response, ...errorJson };
-          if (error.stack) {
-            error = {
-              ...error,
-              error:
-                t('Unexpected error: ') +
-                (error.description ||
-                  t('(no description, click to see stack trace)')),
-              stacktrace: error.stack,
-            };
-          } else if (
-            error.responseText &&
-            error.responseText.indexOf('CSRF') >= 0
-          ) {
-            error = {
-              ...error,
-              error: t(COMMON_ERR_MESSAGES.SESSION_TIMED_OUT),
-            };
-          }
-          resolve(error);
-        })
-        .catch(() => {
-          // fall back to reading as text
-          response.text().then(errorText => {
-            resolve({ ...response, error: errorText });
-          });
-        });
-    } else {
-      // fall back to Response.statusText or generic error of we cannot read 
the response
-      resolve({
-        ...response,
-        error: response.statusText || t('An error occurred'),
-      });
-    }
-  });
-}
diff --git a/superset-frontend/src/utils/getClientErrorObject.ts 
b/superset-frontend/src/utils/getClientErrorObject.ts
new file mode 100644
index 0000000..8ca3605
--- /dev/null
+++ b/superset-frontend/src/utils/getClientErrorObject.ts
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClientResponse } from '@superset-ui/connection';
+import { t } from '@superset-ui/translation';
+import COMMON_ERR_MESSAGES from './errorMessages';
+
+// The response always contains an error attribute, can contain anything from 
the
+// SupersetClientResponse object, and can contain a spread JSON blob
+export type ClientErrorObject = {
+  error: string;
+  severity?: string;
+  message?: string;
+  stacktrace?: string;
+} & Partial<SupersetClientResponse>;
+
+export default function getClientErrorObject(
+  response: SupersetClientResponse | string,
+): Promise<ClientErrorObject> {
+  // takes a SupersetClientResponse as input, attempts to read response as 
Json if possible,
+  // and returns a Promise that resolves to a plain object with error key and 
text value.
+  return new Promise(resolve => {
+    if (typeof response === 'string') {
+      resolve({ error: response });
+    } else {
+      const responseObject =
+        response instanceof Response ? response : response.response;
+      if (responseObject && !responseObject.bodyUsed) {
+        // attempt to read the body as json, and fallback to text. we must 
clone the
+        // response in order to fallback to .text() because Response is 
single-read
+        responseObject
+          .clone()
+          .json()
+          .then(errorJson => {
+            let error = { ...responseObject, ...errorJson };
+            if (error.stack) {
+              error = {
+                ...error,
+                error:
+                  t('Unexpected error: ') +
+                  (error.description ||
+                    t('(no description, click to see stack trace)')),
+                stacktrace: error.stack,
+              };
+            } else if (
+              error.responseText &&
+              error.responseText.indexOf('CSRF') >= 0
+            ) {
+              error = {
+                ...error,
+                error: t(COMMON_ERR_MESSAGES.SESSION_TIMED_OUT),
+              };
+            }
+            resolve(error);
+          })
+          .catch(() => {
+            // fall back to reading as text
+            responseObject.text().then(errorText => {
+              resolve({ ...responseObject, error: errorText });
+            });
+          });
+      } else {
+        // fall back to Response.statusText or generic error of we cannot read 
the response
+        const error =
+          'statusText' in response
+            ? response.statusText
+            : t('An error occurred');
+        resolve({
+          ...responseObject,
+          error,
+        });
+      }
+    }
+  });
+}

Reply via email to