@@ -4,8 +4,8 @@ import { container } from 'tsyringe';
4
4
import { SpecificationFile } from '../validation' ;
5
5
6
6
export type Result = {
7
- response ?: any ,
8
- error ?: Error
7
+ response ?: any ,
8
+ error ?: Error
9
9
}
10
10
11
11
export const useContextFile = ( ) : any => {
@@ -24,7 +24,7 @@ export const useContextFile = (): any => {
24
24
current : ( ) : Result => {
25
25
try {
26
26
const ctx : Context = contextService . loadContextFile ( ) ;
27
- if ( ! ctx . current ) { throw new MissingCurrentContextError ( ) ; }
27
+ if ( ! ctx . current ) { throw new MissingCurrentContextError ( ) ; }
28
28
const response = { key : ctx . current , path : ctx . store [ ctx . current ] } ;
29
29
return { response } ;
30
30
} catch ( error ) {
@@ -41,10 +41,14 @@ export const useContextFile = (): any => {
41
41
} catch ( error ) {
42
42
if ( error instanceof ContextFileNotFoundError ) {
43
43
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
+ }
48
52
}
49
53
return { error } ;
50
54
}
@@ -98,7 +102,7 @@ export const useContextFile = (): any => {
98
102
try {
99
103
const ctx = contextService . loadContextFile ( ) ;
100
104
const ctxValue = ctx . store [ String ( key ) ] ;
101
- if ( ! ctxValue ) { throw new ContextNotFoundError ( ) ; }
105
+ if ( ! ctxValue ) { throw new ContextNotFoundError ( ) ; }
102
106
const response = new SpecificationFile ( ctxValue ) ;
103
107
return { response } ;
104
108
} catch ( error ) {
@@ -109,13 +113,13 @@ export const useContextFile = (): any => {
109
113
} ;
110
114
111
115
export interface useSpecFileInput {
112
- file ?: string ,
113
- context ?: string
116
+ file ?: string ,
117
+ context ?: string
114
118
}
115
119
116
120
export interface useSpecFileOutput {
117
- specFile ?: SpecificationFile ,
118
- error ?: Error
121
+ specFile ?: SpecificationFile ,
122
+ error ?: Error
119
123
}
120
124
121
125
export const useSpecfile = ( flags : useSpecFileInput ) : useSpecFileOutput => {
@@ -124,29 +128,29 @@ export const useSpecfile = (flags: useSpecFileInput): useSpecFileOutput => {
124
128
try {
125
129
if ( flags . file ) {
126
130
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' ) ; }
128
132
return { specFile } ;
129
133
}
130
134
131
135
const ctx : Context = contextService . loadContextFile ( ) ;
132
136
133
137
if ( flags . context ) {
134
138
const ctxFile = ctx . store [ flags . context ] ;
135
- if ( ! ctxFile ) { throw new ContextNotFoundError ( ) ; }
139
+ if ( ! ctxFile ) { throw new ContextNotFoundError ( ) ; }
136
140
const specFile = new SpecificationFile ( ctxFile ) ;
137
141
return { specFile } ;
138
142
}
139
143
140
144
if ( ctx . current ) {
141
145
const currentFile = ctx . store [ ctx . current ] ;
142
- if ( ! currentFile ) { throw new MissingCurrentContextError ( ) ; }
146
+ if ( ! currentFile ) { throw new MissingCurrentContextError ( ) ; }
143
147
const specFile = new SpecificationFile ( currentFile ) ;
144
148
return { specFile } ;
145
149
}
146
150
147
151
const autoDetectedSpecPath = contextService . autoDetectSpecFile ( ) ;
148
152
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' ) ; }
150
154
151
155
const specFile = new SpecificationFile ( autoDetectedSpecPath ) ;
152
156
0 commit comments