feat(repos): 获取 commit logs 时,允许返回错误信息给调用方。

This commit is contained in:
Ivan Li 2021-06-27 10:33:32 +08:00
parent 9d735c582c
commit 7e17de0f15
3 changed files with 22 additions and 12 deletions

View File

@ -1,11 +1,11 @@
import { pick } from 'ramda';
export class ApplicationException extends Error { export class ApplicationException extends Error {
code: number; code: number;
error: Error; error: Error;
constructor( constructor(
message: message: string | { error?: Error; message?: string | any; code?: number },
| string
| { error?: Error; message?: string | object; code?: number },
) { ) {
if (message instanceof Object) { if (message instanceof Object) {
super(); super();
@ -18,4 +18,8 @@ export class ApplicationException extends Error {
super((message as unknown) as any); super((message as unknown) as any);
} }
} }
toJSON() {
return pick(['code', 'message'], this);
}
} }

View File

@ -1,10 +1,12 @@
import { ObjectType, Field } from '@nestjs/graphql'; import { ObjectType, Field } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { LogResult, DefaultLogFields } from 'simple-git'; import { LogResult, DefaultLogFields } from 'simple-git';
import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity'; import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity';
@ObjectType() @ObjectType()
export class Commit { export class Commit {
hash: string; hash: string;
@Type(() => Date)
date: Date; date: Date;
message: string; message: string;
refs: string; refs: string;

View File

@ -26,6 +26,7 @@ import {
getInstanceName, getInstanceName,
getSelfInstanceRouteKey, getSelfInstanceRouteKey,
} from '../commons/utils/rabbit-mq'; } from '../commons/utils/rabbit-mq';
import { ApplicationException } from '../commons/exceptions/application.exception';
const DEFAULT_REMOTE_NAME = 'origin'; const DEFAULT_REMOTE_NAME = 'origin';
const INFO_PATH = '@info'; const INFO_PATH = '@info';
@ -129,7 +130,7 @@ export class ReposService {
autoDelete: true, autoDelete: true,
}, },
}) })
async listCommits(pipeline: Pipeline): Promise<Commit[] | Nack> { async listCommits(pipeline: Pipeline): Promise<[Error, Commit[]?]> {
const git = await this.getGit(pipeline.project, undefined, { const git = await this.getGit(pipeline.project, undefined, {
fetch: false, fetch: false,
}); });
@ -140,20 +141,23 @@ export class ReposService {
`remotes/origin/${pipeline.branch}`, `remotes/origin/${pipeline.branch}`,
'--', '--',
]); ]);
return data.all.map( return [
(it) => null,
({ data.all.map(
...it, (it) =>
date: new Date(it.date), ({
} as Commit), ...it,
); date: new Date(it.date),
} as Commit),
),
];
} catch (error) { } catch (error) {
this.logger.error( this.logger.error(
{ error, pipeline }, { error, pipeline },
'[listCommits] %s', '[listCommits] %s',
error?.message, error?.message,
); );
return new Nack(); return [new ApplicationException(error)];
} }
} }