Skip to content

Commit aa172bb

Browse files
committed
refactor
1 parent ab3e8ce commit aa172bb

File tree

5 files changed

+16
-107
lines changed

5 files changed

+16
-107
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ hawk.setContext({
142142
version: '2.1.0',
143143
environment: 'production',
144144
});
145-
146-
// Clear context data
147-
hawk.clearContext();
148145
```
149146
150147
## Source maps consuming

example/index.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ <h1>Hawk JavaScript Catcher</h1>
100100
<section>
101101
<h2>Send test event</h2>
102102
<button onclick="window.hawk.test()">hawk.test()</button>
103-
<br /><br />
104-
<button id="btn-test-without-user">Test without user</button>
105103
</section>
106104
<section>
107105
<h2>Send real error</h2>
@@ -175,7 +173,6 @@ <h2>Context Management</h2>
175173
<input type="text" id="contextValue" placeholder="user-dashboard" />
176174
<br /><br />
177175
<button id="btn-set-context">Set Context</button>
178-
<button id="btn-clear-context">Clear Context</button>
179176
</section>
180177
<script src="https://unpkg.com/vue@2"></script>
181178
<section>

example/sample-errors.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ buttonClearUser.addEventListener('click', () => {
107107
* Context Management
108108
*/
109109
const buttonSetContext = document.getElementById('btn-set-context');
110-
const buttonClearContext = document.getElementById('btn-clear-context');
111110

112111
buttonSetContext.addEventListener('click', () => {
113112
const contextKey = document.getElementById('contextKey').value;
@@ -124,20 +123,3 @@ buttonSetContext.addEventListener('click', () => {
124123

125124
window.hawk.setContext(context);
126125
});
127-
128-
buttonClearContext.addEventListener('click', () => {
129-
window.hawk.clearContext();
130-
});
131-
132-
/**
133-
* Test without user
134-
*/
135-
const buttonTestWithoutUser = document.getElementById('btn-test-without-user');
136-
137-
buttonTestWithoutUser.addEventListener('click', () => {
138-
// Clear user first to ensure no user is set
139-
window.hawk.clearUser();
140-
141-
// Send error without user
142-
window.hawk.send(new Error('Test error without user'));
143-
});

src/catcher.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { EventRejectedError } from './errors';
1717
import type { HawkJavaScriptEvent } from './types';
1818
import { isErrorProcessed, markErrorAsProcessed } from './utils/event';
1919
import { addErrorEvent, getConsoleLogStack, initConsoleCatcher } from './addons/consoleCatcher';
20-
import { validateUser, validateContext, logValidationErrors } from './utils/validation';
20+
import { validateUser, validateContext } from './utils/validation';
2121

2222
/**
2323
* Allow to use global VERSION, that will be overwritten by Webpack
@@ -238,15 +238,11 @@ export default class Catcher {
238238
* @param user - New user information
239239
*/
240240
public setUser(user: AffectedUser): void {
241-
const validation = validateUser(user);
242-
243-
if (!validation.isValid) {
244-
logValidationErrors('setUser', validation.errors);
245-
241+
if (!validateUser(user)) {
246242
return;
247243
}
248244

249-
this.user = validation.data!;
245+
this.user = user;
250246
}
251247

252248
/**
@@ -262,22 +258,11 @@ export default class Catcher {
262258
* @param context - New context data
263259
*/
264260
public setContext(context: EventContext): void {
265-
const validation = validateContext(context);
266-
267-
if (!validation.isValid) {
268-
logValidationErrors('setContext', validation.errors);
269-
261+
if (!validateContext(context)) {
270262
return;
271263
}
272264

273-
this.context = validation.data!;
274-
}
275-
276-
/**
277-
* Clear current context data
278-
*/
279-
public clearContext(): void {
280-
this.context = undefined;
265+
this.context = context;
281266
}
282267

283268
/**

src/utils/validation.ts

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,40 @@
1+
import log from './log';
12
import type { AffectedUser, EventContext } from '@hawk.so/types';
23
import Sanitizer from '../modules/sanitizer';
34

4-
/**
5-
* Validation result interface
6-
*/
7-
interface ValidationResult<T> {
8-
isValid: boolean;
9-
data?: T;
10-
errors: string[];
11-
}
12-
135
/**
146
* Validates user data - basic security checks
157
*
168
* @param user
179
*/
18-
export function validateUser(user: AffectedUser): ValidationResult<AffectedUser> {
19-
const errors: string[] = [];
20-
10+
export function validateUser(user: AffectedUser): boolean {
2111
if (!user || !Sanitizer.isObject(user)) {
22-
errors.push('User must be an object');
12+
log('validateUser: User must be an object', 'warn');
2313

24-
return { isValid: false,
25-
errors };
14+
return false;
2615
}
2716

2817
// Validate required ID
2918
if (!user.id || typeof user.id !== 'string' || user.id.trim() === '') {
30-
errors.push('User ID is required and must be a non-empty string');
31-
32-
return { isValid: false,
33-
errors };
34-
}
35-
36-
const validatedUser: AffectedUser = {
37-
id: user.id.trim(),
38-
};
39-
40-
// Add optional fields if they exist and are strings
41-
if (user.name && typeof user.name === 'string') {
42-
validatedUser.name = user.name.trim();
43-
}
44-
45-
if (user.url && typeof user.url === 'string') {
46-
validatedUser.url = user.url.trim();
47-
}
19+
log('validateUser: User ID is required and must be a non-empty string', 'warn');
4820

49-
if (user.image && typeof user.image === 'string') {
50-
validatedUser.image = user.image.trim();
21+
return false;
5122
}
5223

53-
return {
54-
isValid: true,
55-
data: validatedUser,
56-
errors,
57-
};
24+
return true;
5825
}
5926

6027
/**
6128
* Validates context data - basic security checks
6229
*
6330
* @param context
6431
*/
65-
export function validateContext(context: EventContext): ValidationResult<EventContext> {
66-
const errors: string[] = [];
67-
32+
export function validateContext(context: EventContext): boolean {
6833
if (!context || !Sanitizer.isObject(context)) {
69-
errors.push('Context must be an object');
34+
log('validateContext: Context must be an object', 'warn');
7035

71-
return { isValid: false,
72-
errors };
36+
return false;
7337
}
7438

75-
return {
76-
isValid: true,
77-
data: context,
78-
errors,
79-
};
80-
}
81-
82-
/**
83-
* Logs validation errors
84-
*
85-
* @param prefix
86-
* @param errors
87-
*/
88-
export function logValidationErrors(prefix: string, errors: string[]): void {
89-
errors.forEach((error) => {
90-
console.warn(`${prefix}: ${error}`);
91-
});
39+
return true;
9240
}

0 commit comments

Comments
 (0)