Răsfoiți Sursa

change ApiResponse type

yuto-o 4 ani în urmă
părinte
comite
c77391f1e3

+ 2 - 2
packages/app/src/client/services/AppContainer.js

@@ -330,9 +330,9 @@ export default class AppContainer extends Container {
       return res.data;
     }
 
-    // Return error code if code is exist
+    // Return error code and data if error code exists
     if (res.data.code != null) {
-      const error = new Apiv1ErrorHandler(res.data.error, res.data.code);
+      const error = new Apiv1ErrorHandler(res.data.error, res.data.code, res.data.data);
       throw error;
     }
 

+ 0 - 5
packages/app/src/client/services/PageContainer.js

@@ -495,11 +495,6 @@ export default class PageContainer extends Container {
     if (!res.ok) {
       throw new Error(res.error);
     }
-    if (res.isPageNotUpdatable) {
-      const err = new Error();
-      err.data = res.data;
-      throw err;
-    }
     return res;
   }
 

+ 2 - 1
packages/app/src/client/util/apiv1ErrorHandler.js

@@ -1,10 +1,11 @@
 class Apiv1ErrorHandler extends Error {
 
-  constructor(message = '', code = '') {
+  constructor(message = '', code = '', data = '') {
     super();
 
     this.message = message;
     this.code = code;
+    this.data = data;
   }
 
 }

+ 5 - 4
packages/app/src/server/routes/page.js

@@ -831,9 +831,10 @@ module.exports = function(crowi, app) {
     const Revision = crowi.model('Revision');
     let page = await Page.findByIdAndViewer(pageId, req.user);
     if (page != null && revisionId != null && !page.isUpdatable(revisionId)) {
-      // when isUpdatable is false, originRevision is the reqested revision
-      const originRevision = await Revision.findById(revisionId).populate('author');
-      const latestRevision = await Revision.findLatestRevisionByPathPopulatedWithAuthor(originRevision.path);
+      const populatedFields = 'name imageUrlCached';
+      // when isUpdatable is false, originRevisionId is a reqested revisionId
+      const originRevision = await Revision.findById(revisionId).populate('author', populatedFields);
+      const latestRevision = await Revision.findById(page.revision).populate('author', populatedFields);
 
       const revisions = {};
 
@@ -858,7 +859,7 @@ module.exports = function(crowi, app) {
         userName: latestRevision.author.name,
         userImgPath: latestRevision.author.imageUrlCached,
       };
-      return res.json(ApiResponse.success({ isPageNotUpdatable: true, data: revisions }));
+      return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'conflict', revisions));
     }
 
     const options = { isSyncRevisionToHackmd };

+ 2 - 1
packages/app/src/server/util/apiResponse.js

@@ -1,11 +1,12 @@
 function ApiResponse() {
 }
 
-ApiResponse.error = function(err, code) {
+ApiResponse.error = function(err, code, data) {
   const result = {};
 
   result.ok = false;
   result.code = code;
+  result.data = data;
 
   if (err instanceof Error) {
     result.error = err.toString();