@@ -15,7 +15,12 @@ router.post('/:collectionName', verifyApiKey, async (req, res) => {
1515 const currentProject = await Project . findById ( project . _id ) ;
1616
1717 const collectionConfig = currentProject . collections . find ( c => c . name === collectionName ) ;
18- if ( ! collectionConfig ) return res . status ( 404 ) . send ( `Collection '${ collectionName } ' not found.` ) ;
18+ if ( ! collectionConfig ) {
19+ return res . status ( 404 ) . json ( {
20+ error : "Collection not found" ,
21+ collection : collectionName
22+ } ) ;
23+ }
1924
2025 const schemaRules = collectionConfig . model ;
2126 const incomingData = req . body ;
@@ -24,7 +29,10 @@ router.post('/:collectionName', verifyApiKey, async (req, res) => {
2429 // --- VALIDATION & SANITIZATION ---
2530 for ( const field of schemaRules ) {
2631 if ( field . required && incomingData [ field . key ] === undefined ) {
27- return res . status ( 400 ) . send ( `Field '${ field . key } ' is required.` ) ;
32+ return res . status ( 400 ) . json ( {
33+ error : `Field '${ field . key } ' is required.` ,
34+ field : field . key
35+ } ) ;
2836 }
2937 if ( incomingData [ field . key ] !== undefined ) {
3038 // Type checking logic (same as before)...
@@ -59,7 +67,8 @@ router.post('/:collectionName', verifyApiKey, async (req, res) => {
5967 } ) ;
6068
6169 } catch ( err ) {
62- res . status ( 500 ) . send ( err . message ) ;
70+ console . log ( err )
71+ res . status ( 500 ) . send ( 'Some issue at our end' ) ;
6372 }
6473} ) ;
6574
@@ -73,7 +82,10 @@ router.get('/:collectionName', verifyApiKey, async (req, res) => {
7382
7483 const collectionConfig = project . collections . find ( c => c . name === collectionName ) ;
7584 if ( ! collectionConfig ) {
76- return res . status ( 404 ) . send ( `Collection '${ collectionName } ' not found.` ) ;
85+ return res . status ( 404 ) . json ( {
86+ error : "Collection not found" ,
87+ collection : collectionName
88+ } ) ;
7789 }
7890 const finalCollectionName = `${ project . _id } _${ collectionName } ` ;
7991
@@ -85,7 +97,8 @@ router.get('/:collectionName', verifyApiKey, async (req, res) => {
8597 res . json ( data ) ;
8698
8799 } catch ( err ) {
88- res . status ( 500 ) . send ( err . message ) ;
100+ console . log ( err )
101+ res . status ( 500 ) . send ( 'Some issue at our end' ) ;
89102 }
90103} ) ;
91104
@@ -99,7 +112,12 @@ router.get('/:collectionName/:id', verifyApiKey, async (req, res) => {
99112
100113 // Security Check
101114 const collectionConfig = project . collections . find ( c => c . name === collectionName ) ;
102- if ( ! collectionConfig ) return res . status ( 404 ) . send ( `Collection not found.` ) ;
115+ if ( ! collectionConfig ) {
116+ return res . status ( 404 ) . json ( {
117+ error : "Collection not found" ,
118+ collection : collectionName
119+ } ) ;
120+ }
103121
104122 const finalCollectionName = `${ project . _id } _${ collectionName } ` ;
105123
@@ -115,8 +133,8 @@ router.get('/:collectionName/:id', verifyApiKey, async (req, res) => {
115133 res . json ( doc ) ;
116134
117135 } catch ( err ) {
118- // Agar ID format galat hai toh MongoDB error dega
119- res . status ( 400 ) . send ( "Invalid ID format or Server Error: " + err . message ) ;
136+ console . log ( err )
137+ res . status ( 500 ) . send ( 'Some issue at our end' ) ;
120138 }
121139} ) ;
122140
@@ -153,7 +171,8 @@ router.delete('/:collectionName/:id', verifyApiKey, async (req, res) => {
153171 res . json ( { message : "Document deleted" , id } ) ;
154172
155173 } catch ( err ) {
156- res . status ( 400 ) . send ( "Error: " + err . message ) ;
174+ console . log ( err )
175+ res . status ( 500 ) . send ( 'Some issue at our end' ) ;
157176 }
158177} ) ;
159178
@@ -169,11 +188,14 @@ router.put('/:collectionName/:id', verifyApiKey, async (req, res) => {
169188
170189 // Security & Config Find
171190 const collectionConfig = project . collections . find ( c => c . name === collectionName ) ;
172- if ( ! collectionConfig ) return res . status ( 404 ) . send ( `Collection not found.` ) ;
173-
191+ if ( ! collectionConfig ) {
192+ return res . status ( 404 ) . json ( {
193+ error : "Collection not found" ,
194+ collection : collectionName
195+ } ) ;
196+ }
174197 // --- VALIDATION REPEAT (Zaroori hai) ---
175198 const schemaRules = collectionConfig . model ;
176- // Hum loop chala kar check karenge ki jo fields update ho rahe hain woh sahi hain ya nahi
177199 for ( const key in incomingData ) {
178200 // 1. Check if field exists in schema
179201 const fieldRule = schemaRules . find ( f => f . key === key ) ;
@@ -210,7 +232,8 @@ router.put('/:collectionName/:id', verifyApiKey, async (req, res) => {
210232 res . json ( { message : "Document updated successfully" , id, updatedFields : incomingData } ) ;
211233
212234 } catch ( err ) {
213- res . status ( 400 ) . send ( "Invalid ID format, Validation Error or Server Error: " + err . message ) ;
235+ console . log ( err )
236+ res . status ( 500 ) . send ( 'Some issue at our end' ) ;
214237 }
215238} ) ;
216239
0 commit comments