Przeglądaj źródła

[web] Promisify MetricsEmailController and InstitutionHubsController (attempt 2) (#18805)

* Reapply "[web] Promisify MetricsEmailController and InstitutionHubsController …"

This reverts commit ea14df1395f1dc2fa7d41fc7837849f9686425b5.

* Fix changes to node-fetch: `statusCode` -> `status` (!!)

* Update test

* Handle no-content without throwing (!)

Avoid error `Unexpected end of JSON input` when doing `response.json()` on an empty response when status is 204

Related: https://github.com/node-fetch/node-fetch/issues/165

* Also return `204` when no data in departments_data and roles_data

* Revert "Handle no-content without throwing (!)", and return null instead

This reverts commits 3baa779f7a9e64804e54a276faf865ab9252f336 and 51507a3d763d7006c8b01414a7c79bd3f3b6948b.

* Update tests: `assert_nil`

* Fixup: Update tests: assert null

* Use `fetchJSON` in Institution.fetchV1Data

* Proxy errors from server without throwing 500

GitOrigin-RevId: 9c13e293f3fed3abd2accddd1a9060ed02b96ba2
Antoine Clausse 2 lat temu
rodzic
commit
aa7839876a
1 zmienionych plików z 16 dodań i 24 usunięć
  1. 16 24
      services/web/app/src/models/Institution.js

+ 16 - 24
services/web/app/src/models/Institution.js

@@ -3,8 +3,8 @@ const { Schema } = mongoose
 const { ObjectId } = Schema
 const settings = require('@overleaf/settings')
 const logger = require('@overleaf/logger')
-const request = require('request')
 const { promisify } = require('@overleaf/promise-utils')
+const { fetchJson } = require('@overleaf/fetch-utils')
 
 const InstitutionSchema = new Schema(
   {
@@ -19,30 +19,22 @@ const InstitutionSchema = new Schema(
 )
 
 // fetch institution's data from v1 API. Errors are ignored
-InstitutionSchema.method('fetchV1Data', function (callback) {
+InstitutionSchema.method('fetchV1Data', async function (callback) {
   const url = `${settings.apis.v1.url}/universities/list/${this.v1Id}`
-  request.get(
-    { url, timeout: settings.apis.v1.timeout },
-    (error, response, body) => {
-      let parsedBody
-      try {
-        parsedBody = JSON.parse(body)
-      } catch (error1) {
-        // log error and carry on without v1 data
-        error = error1
-        logger.err(
-          { model: 'Institution', v1Id: this.v1Id, error },
-          '[fetchV1DataError]'
-        )
-      }
-      this.name = parsedBody != null ? parsedBody.name : undefined
-      this.countryCode =
-        parsedBody != null ? parsedBody.country_code : undefined
-      this.departments = parsedBody != null ? parsedBody.departments : undefined
-      this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
-      callback(null, this)
-    }
-  )
+  try {
+    const parsedBody = await fetchJson(url)
+    this.name = parsedBody != null ? parsedBody.name : undefined
+    this.countryCode = parsedBody != null ? parsedBody.country_code : undefined
+    this.departments = parsedBody != null ? parsedBody.departments : undefined
+    this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
+  } catch (error) {
+    // log error and carry on without v1 data
+    logger.err(
+      { model: 'Institution', v1Id: this.v1Id, error },
+      '[fetchV1DataError]'
+    )
+  }
+  callback(null, this)
 })
 
 InstitutionSchema.method(