Yuki Takei 3 лет назад
Родитель
Сommit
9d7f0b9dc2

+ 11 - 6
packages/remark-lsx/src/server/routes/lsx.js

@@ -1,3 +1,5 @@
+import createError, { isHttpError } from 'http-errors';
+
 const { customTagUtils } = require('@growi/core');
 
 const { OptionParser } = customTagUtils;
@@ -21,7 +23,7 @@ class Lsx {
   static addDepthCondition(query, pagePath, optionsDepth) {
     // when option strings is 'depth=', the option value is true
     if (optionsDepth == null || optionsDepth === true) {
-      throw new Error('The value of depth option is invalid.');
+      throw createError(400, 'The value of depth option is invalid.');
     }
 
     const range = OptionParser.parseRange(optionsDepth);
@@ -29,7 +31,7 @@ class Lsx {
     const end = range.end;
 
     if (start < 1 || end < 1) {
-      throw new Error(`specified depth is [${start}:${end}] : start and end are must be larger than 1`);
+      throw createError(400, `specified depth is [${start}:${end}] : start and end are must be larger than 1`);
     }
 
     // count slash
@@ -56,7 +58,7 @@ class Lsx {
   static addNumCondition(query, pagePath, optionsNum) {
     // when option strings is 'num=', the option value is true
     if (optionsNum == null || optionsNum === true) {
-      throw new Error('The value of num option is invalid.');
+      throw createError(400, 'The value of num option is invalid.');
     }
 
     if (typeof optionsNum === 'number') {
@@ -68,7 +70,7 @@ class Lsx {
     const end = range.end;
 
     if (start < 1 || end < 1) {
-      throw new Error(`specified num is [${start}:${end}] : start and end are must be larger than 1`);
+      throw createError(400, `specified num is [${start}:${end}] : start and end are must be larger than 1`);
     }
 
     const skip = start - 1;
@@ -92,7 +94,7 @@ class Lsx {
   static addFilterCondition(query, pagePath, optionsFilter, isExceptFilter = false) {
     // when option strings is 'filter=', the option value is true
     if (optionsFilter == null || optionsFilter === true) {
-      throw new Error('filter option require value in regular expression.');
+      throw createError(400, 'filter option require value in regular expression.');
     }
 
     let filterPath = '';
@@ -141,7 +143,7 @@ class Lsx {
     const isReversed = optionsReverse === 'true';
 
     if (optionsSort !== 'path' && optionsSort !== 'createdAt' && optionsSort !== 'updatedAt') {
-      throw new Error(`The specified value '${optionsSort}' for the sort option is invalid. It must be 'path', 'createdAt' or 'updatedAt'.`);
+      throw createError(400, `The specified value '${optionsSort}' for the sort option is invalid. It must be 'path', 'createdAt' or 'updatedAt'.`);
     }
 
     const sortOption = {};
@@ -245,6 +247,9 @@ module.exports = (crowi, app) => {
       res.status(200).send({ pages, toppageViewersCount });
     }
     catch (error) {
+      if (isHttpError) {
+        return res.status(error.status).send(error);
+      }
       return res.status(500).send(error);
     }
   };

+ 4 - 2
packages/remark-lsx/src/stores/lsx.tsx

@@ -124,10 +124,12 @@ type LsxNodeTree = {
 export const useSWRxNodeTree = (lsxContext: LsxContext, isImmutable?: boolean): SWRResponse<LsxNodeTree, Error> => {
   const { data, error } = useSWRxLsxResponse(lsxContext.pagePath, lsxContext.options, isImmutable);
 
+  const isLoading = data === undefined && error == null;
+
   return useSWR(
-    data === undefined ? null : ['lsxNodeTree', lsxContext.pagePath, lsxContext.options, isImmutable, data],
+    !isLoading ? ['lsxNodeTree', lsxContext.pagePath, lsxContext.options, isImmutable, data, error] : null,
     (key, pagePath, options, isImmutable, data) => {
-      if (error != null) {
+      if (data === undefined || error != null) {
         throw error;
       }
       return {