Skip to content

Commit dfca5bc

Browse files
CopilotBaseMax
andcommitted
Add integration examples and fix code review issues
Co-authored-by: BaseMax <2658040+BaseMax@users.noreply.github.com>
1 parent 191ec4f commit dfca5bc

File tree

8 files changed

+480
-5
lines changed

8 files changed

+480
-5
lines changed

examples/README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Error Vault Integration Examples
2+
3+
This directory contains example client implementations for integrating Error Vault with applications in various programming languages.
4+
5+
## Directory Structure
6+
7+
```
8+
examples/
9+
├── go/ # Go client example
10+
├── nodejs/ # Node.js client example
11+
└── python/ # Python client example
12+
```
13+
14+
## Go Example
15+
16+
### Setup
17+
18+
```bash
19+
cd examples/go
20+
go mod init example
21+
go get github.com/BaseMax/go-error-vault
22+
go run client.go
23+
```
24+
25+
### Usage
26+
27+
```go
28+
client := NewErrorVaultClient(
29+
"http://localhost:8080/api/errors",
30+
"my-go-app",
31+
"1.0.0",
32+
)
33+
34+
err := fmt.Errorf("something went wrong")
35+
metadata := map[string]interface{}{
36+
"user_id": "12345",
37+
}
38+
39+
client.ReportError(err, metadata)
40+
```
41+
42+
## Node.js Example
43+
44+
### Setup
45+
46+
```bash
47+
cd examples/nodejs
48+
npm install
49+
node client.js
50+
```
51+
52+
### Usage
53+
54+
```javascript
55+
const ErrorVaultClient = require('./client');
56+
57+
const client = new ErrorVaultClient(
58+
'http://localhost:8080/api/errors',
59+
'my-node-app',
60+
'1.0.0'
61+
);
62+
63+
try {
64+
// Your code
65+
} catch (error) {
66+
await client.reportError(error, {
67+
user_id: '12345'
68+
});
69+
}
70+
```
71+
72+
## Python Example
73+
74+
### Setup
75+
76+
```bash
77+
cd examples/python
78+
pip install -r requirements.txt
79+
python client.py
80+
```
81+
82+
### Usage
83+
84+
```python
85+
from client import ErrorVaultClient
86+
87+
client = ErrorVaultClient(
88+
url='http://localhost:8080/api/errors',
89+
app_name='my-python-app',
90+
app_version='1.0.0'
91+
)
92+
93+
try:
94+
# Your code
95+
except Exception as error:
96+
client.report_error(error, {
97+
'user_id': '12345'
98+
})
99+
```
100+
101+
## File-Based Integration
102+
103+
You can also report errors by dropping JSON files into the Error Vault watch directory:
104+
105+
```bash
106+
# Default watch directory
107+
mkdir -p ~/.errorvault/incoming
108+
109+
# Create an error report
110+
cat > ~/.errorvault/incoming/error.json << EOF
111+
{
112+
"message": "Database connection timeout",
113+
"stack": "at db.connect() line 42",
114+
"app_name": "myapp",
115+
"app_version": "1.0.0",
116+
"metadata": {
117+
"database": "users",
118+
"timeout": 5000
119+
}
120+
}
121+
EOF
122+
```
123+
124+
The file will be automatically processed within 5 seconds (default collection interval) and moved to `.processed/` directory.
125+
126+
## Integration Best Practices
127+
128+
1. **Always include metadata**: Add contextual information like user IDs, request IDs, or environment details
129+
2. **Don't block on errors**: Report errors asynchronously to avoid impacting application performance
130+
3. **Handle reporting failures gracefully**: Log locally if Error Vault is unavailable
131+
4. **Include stack traces**: Full stack traces help with debugging and grouping
132+
5. **Set appropriate app versions**: This helps track when issues were introduced
133+
134+
## Error Report Format
135+
136+
All errors should follow this JSON schema:
137+
138+
```json
139+
{
140+
"message": "Error description",
141+
"stack": "Stack trace (optional but recommended)",
142+
"app_name": "Your application name",
143+
"app_version": "1.0.0 (optional)",
144+
"metadata": {
145+
"key": "value",
146+
"custom": "fields"
147+
}
148+
}
149+
```
150+
151+
## Testing Your Integration
152+
153+
1. Start Error Vault server:
154+
```bash
155+
errorvault server
156+
```
157+
158+
2. Run your client example
159+
160+
3. Verify the error was received:
161+
```bash
162+
errorvault list
163+
```
164+
165+
## Support
166+
167+
For more information, see the main [README](../README.md).

examples/go/client.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"runtime/debug"
10+
)
11+
12+
// ErrorReport represents an error to be sent to Error Vault
13+
type ErrorReport struct {
14+
Message string `json:"message"`
15+
Stack string `json:"stack"`
16+
AppName string `json:"app_name"`
17+
AppVersion string `json:"app_version"`
18+
Metadata map[string]interface{} `json:"metadata,omitempty"`
19+
}
20+
21+
// ErrorVaultClient is a client for reporting errors to Error Vault
22+
type ErrorVaultClient struct {
23+
url string
24+
appName string
25+
appVersion string
26+
}
27+
28+
// NewErrorVaultClient creates a new Error Vault client
29+
func NewErrorVaultClient(url, appName, appVersion string) *ErrorVaultClient {
30+
return &ErrorVaultClient{
31+
url: url,
32+
appName: appName,
33+
appVersion: appVersion,
34+
}
35+
}
36+
37+
// ReportError sends an error to Error Vault
38+
func (c *ErrorVaultClient) ReportError(err error, metadata map[string]interface{}) error {
39+
report := ErrorReport{
40+
Message: err.Error(),
41+
Stack: string(debug.Stack()),
42+
AppName: c.appName,
43+
AppVersion: c.appVersion,
44+
Metadata: metadata,
45+
}
46+
47+
data, jsonErr := json.Marshal(report)
48+
if jsonErr != nil {
49+
return fmt.Errorf("failed to marshal error report: %w", jsonErr)
50+
}
51+
52+
resp, httpErr := http.Post(c.url, "application/json", bytes.NewBuffer(data))
53+
if httpErr != nil {
54+
return fmt.Errorf("failed to send error report: %w", httpErr)
55+
}
56+
defer resp.Body.Close()
57+
58+
if resp.StatusCode != http.StatusCreated {
59+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
60+
}
61+
62+
return nil
63+
}
64+
65+
// Example usage
66+
func main() {
67+
// Initialize the client
68+
client := NewErrorVaultClient(
69+
"http://localhost:8080/api/errors",
70+
"my-go-app",
71+
"1.0.0",
72+
)
73+
74+
// Simulate an error
75+
err := fmt.Errorf("database connection failed")
76+
77+
// Report the error with metadata
78+
metadata := map[string]interface{}{
79+
"user_id": "12345",
80+
"endpoint": "/api/users",
81+
"method": "GET",
82+
}
83+
84+
if reportErr := client.ReportError(err, metadata); reportErr != nil {
85+
log.Printf("Failed to report error: %v", reportErr)
86+
} else {
87+
log.Println("Error reported successfully")
88+
}
89+
}

examples/nodejs/client.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const axios = require('axios');
2+
3+
/**
4+
* Error Vault client for Node.js applications
5+
*/
6+
class ErrorVaultClient {
7+
constructor(url, appName, appVersion) {
8+
this.url = url;
9+
this.appName = appName;
10+
this.appVersion = appVersion;
11+
}
12+
13+
/**
14+
* Report an error to Error Vault
15+
* @param {Error} error - The error object
16+
* @param {Object} metadata - Additional metadata
17+
*/
18+
async reportError(error, metadata = {}) {
19+
try {
20+
const report = {
21+
message: error.message,
22+
stack: error.stack,
23+
app_name: this.appName,
24+
app_version: this.appVersion,
25+
metadata: metadata
26+
};
27+
28+
const response = await axios.post(this.url, report);
29+
console.log('Error reported:', response.data);
30+
return response.data;
31+
} catch (err) {
32+
console.error('Failed to report error:', err.message);
33+
throw err;
34+
}
35+
}
36+
}
37+
38+
// Example usage
39+
async function main() {
40+
// Initialize the client
41+
const client = new ErrorVaultClient(
42+
'http://localhost:8080/api/errors',
43+
'my-node-app',
44+
'1.0.0'
45+
);
46+
47+
// Set up global error handlers
48+
process.on('uncaughtException', async (error) => {
49+
console.error('Uncaught exception:', error);
50+
await client.reportError(error, {
51+
type: 'uncaughtException'
52+
});
53+
process.exit(1);
54+
});
55+
56+
process.on('unhandledRejection', async (reason, promise) => {
57+
const error = new Error(`Unhandled Rejection: ${reason}`);
58+
console.error('Unhandled rejection:', error);
59+
await client.reportError(error, {
60+
type: 'unhandledRejection',
61+
promise: promise.toString()
62+
});
63+
});
64+
65+
// Simulate an error
66+
try {
67+
throw new Error('Something went wrong in the application');
68+
} catch (error) {
69+
await client.reportError(error, {
70+
user_id: '12345',
71+
endpoint: '/api/users',
72+
method: 'POST'
73+
});
74+
}
75+
}
76+
77+
// Run the example
78+
if (require.main === module) {
79+
main().catch(console.error);
80+
}
81+
82+
module.exports = ErrorVaultClient;

examples/nodejs/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "errorvault-example",
3+
"version": "1.0.0",
4+
"description": "Example Node.js integration with Error Vault",
5+
"main": "client.js",
6+
"scripts": {
7+
"start": "node client.js"
8+
},
9+
"dependencies": {
10+
"axios": "^1.6.0"
11+
}
12+
}

0 commit comments

Comments
 (0)