Skip to content

Commit 9389848

Browse files
authored
fix: hide stacktrace for error on unavailable context (#42)
1 parent 15e7a9c commit 9389848

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/hooks/context/hooks.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { container } from 'tsyringe';
44
import { SpecificationFile } from '../validation';
55

66
export type Result = {
7-
response?: any,
8-
error?: Error
7+
response?: any,
8+
error?: Error
99
}
1010

1111
export const useContextFile = (): any => {
@@ -24,7 +24,7 @@ export const useContextFile = (): any => {
2424
current: (): Result => {
2525
try {
2626
const ctx: Context = contextService.loadContextFile();
27-
if (!ctx.current) {throw new MissingCurrentContextError();}
27+
if (!ctx.current) { throw new MissingCurrentContextError(); }
2828
const response = { key: ctx.current, path: ctx.store[ctx.current] };
2929
return { response };
3030
} catch (error) {
@@ -41,10 +41,14 @@ export const useContextFile = (): any => {
4141
} catch (error) {
4242
if (error instanceof ContextFileNotFoundError) {
4343
const context: Context = { current: '', store: {} };
44-
const newContext = contextService.addContext(context, key, specFile);
45-
contextService.save(contextService.updateCurrent(newContext, key));
46-
const response = 'New context added';
47-
return { response };
44+
try {
45+
const newContext = contextService.addContext(context, key, specFile);
46+
contextService.save(contextService.updateCurrent(newContext, key));
47+
const response = 'New context added';
48+
return { response };
49+
} catch (error) {
50+
return { error };
51+
}
4852
}
4953
return { error };
5054
}
@@ -98,7 +102,7 @@ export const useContextFile = (): any => {
98102
try {
99103
const ctx = contextService.loadContextFile();
100104
const ctxValue = ctx.store[String(key)];
101-
if (!ctxValue) {throw new ContextNotFoundError();}
105+
if (!ctxValue) { throw new ContextNotFoundError(); }
102106
const response = new SpecificationFile(ctxValue);
103107
return { response };
104108
} catch (error) {
@@ -109,13 +113,13 @@ export const useContextFile = (): any => {
109113
};
110114

111115
export interface useSpecFileInput {
112-
file?: string,
113-
context?: string
116+
file?: string,
117+
context?: string
114118
}
115119

116120
export interface useSpecFileOutput {
117-
specFile?: SpecificationFile,
118-
error?: Error
121+
specFile?: SpecificationFile,
122+
error?: Error
119123
}
120124

121125
export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => {
@@ -124,29 +128,29 @@ export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => {
124128
try {
125129
if (flags.file) {
126130
const specFile: SpecificationFile = new SpecificationFile(flags.file);
127-
if (specFile.isNotValid()) {throw new Error('Invalid spec path');}
131+
if (specFile.isNotValid()) { throw new Error('Invalid spec path'); }
128132
return { specFile };
129133
}
130134

131135
const ctx: Context = contextService.loadContextFile();
132136

133137
if (flags.context) {
134138
const ctxFile = ctx.store[flags.context];
135-
if (!ctxFile) {throw new ContextNotFoundError();}
139+
if (!ctxFile) { throw new ContextNotFoundError(); }
136140
const specFile = new SpecificationFile(ctxFile);
137141
return { specFile };
138142
}
139143

140144
if (ctx.current) {
141145
const currentFile = ctx.store[ctx.current];
142-
if (!currentFile) {throw new MissingCurrentContextError();}
146+
if (!currentFile) { throw new MissingCurrentContextError(); }
143147
const specFile = new SpecificationFile(currentFile);
144148
return { specFile };
145149
}
146150

147151
const autoDetectedSpecPath = contextService.autoDetectSpecFile();
148152

149-
if (typeof autoDetectedSpecPath === 'undefined') {throw new Error('No spec path found in your working directory, please use flags or store a context');}
153+
if (typeof autoDetectedSpecPath === 'undefined') { throw new Error('No spec path found in your working directory, please use flags or store a context'); }
150154

151155
const specFile = new SpecificationFile(autoDetectedSpecPath);
152156

0 commit comments

Comments
 (0)