Skip to content

Commit

Permalink
fix: catch unexpected error
Browse files Browse the repository at this point in the history
  • Loading branch information
jzhangdev committed Apr 7, 2024
1 parent a62fec4 commit 2230dc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: catch unexpected error",
"packageName": "@rightcapital/verdaccio-package-diff",
"email": "[email protected]",
"dependentChangeType": "patch"
}
54 changes: 29 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ const textTypeResponse =
next();
};

const internalServerErrorResponse = (
req: Request,
res: Response,
next: NextFunction,
) => {
res.status(500);
next();
};
const internalServerErrorResponse =
(req: Request, res: Response, next: NextFunction) => (error: Error) => {
res.status(500).send({ error: String(error) });
next();
};

export default class VerdaccioMiddlewarePlugin
implements IPluginMiddleware<ICustomConfig>
Expand Down Expand Up @@ -109,29 +106,36 @@ export default class VerdaccioMiddlewarePlugin
const controller = new AbortController();
const { signal } = controller;

const npmDiff = spawnSync(
'npm',
[
'diff',
`--diff=${name as string}@${from as string}`,
`--diff=${name as string}@${to as string}`,
],
{ signal },
);

if (npmDiff.status !== 0) {
textTypeResponse(req, res, next)(npmDiff.stderr.toString());
} else {
textTypeResponse(req, res, next)(npmDiff.stdout.toString());
try {
const npmDiff = spawnSync(
'npm',
[
'diff',
`--diff=${name as string}@${from as string}`,
`--diff=${name as string}@${to as string}`,
],
{ signal },
);
if (npmDiff.status !== 0) {
textTypeResponse(req, res, next)(npmDiff.stderr.toString());
} else {
textTypeResponse(req, res, next)(npmDiff.stdout.toString());
}
} catch (npmDiffUnexpectedError) {
internalServerErrorResponse(
req,
res,
next,
)(npmDiffUnexpectedError as Error);
}
}
},
);
} catch (error) {
if (error instanceof InvalidTokenError) {
} catch (unexpectedError) {
if (unexpectedError instanceof InvalidTokenError) {
unauthorizedResponse(req, res, next);
}
internalServerErrorResponse(req, res, next);
internalServerErrorResponse(req, res, next)(unexpectedError as Error);
}
});

Expand Down

0 comments on commit 2230dc5

Please sign in to comment.