forked from casbin/casbin-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhance error handling with type-specific linters and error par…
…sing (casbin#210)
- Loading branch information
1 parent
02883dd
commit 2fd22b8
Showing
6 changed files
with
148 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import { Diagnostic } from '@codemirror/lint'; | ||
import { EditorView } from '@codemirror/view'; | ||
import { getError } from './errorManager'; | ||
import { ErrorType } from './errorHandler'; | ||
|
||
export const casbinLinter = (view: EditorView): Diagnostic[] => { | ||
const diagnostics: Diagnostic[] = []; | ||
function createLinter(errorType: ErrorType) { | ||
return (view: EditorView): Diagnostic[] => { | ||
const diagnostics: Diagnostic[] = []; | ||
const error = getError(); | ||
|
||
const runTestError = getError(); | ||
if (runTestError) { | ||
const lineMatch = runTestError.match(/line (\d+)/); | ||
if (lineMatch) { | ||
const errorLine = parseInt(lineMatch[1], 10); | ||
const line = view.state.doc.line(errorLine); | ||
diagnostics.push({ | ||
from: line.from, | ||
to: line.to, | ||
severity: 'error', | ||
message: runTestError, | ||
}); | ||
} else { | ||
diagnostics.push({ | ||
from: 0, | ||
to: view.state.doc.length, | ||
severity: 'error', | ||
message: runTestError, | ||
}); | ||
if (error && error.type === errorType) { | ||
if (error.line) { | ||
const line = view.state.doc.line(error.line); | ||
diagnostics.push({ | ||
from: line.from, | ||
to: line.to, | ||
severity: 'error', | ||
message: error.message, | ||
}); | ||
} else { | ||
diagnostics.push({ | ||
from: 0, | ||
to: view.state.doc.length, | ||
severity: 'error', | ||
message: error.message, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return diagnostics; | ||
}; | ||
return diagnostics; | ||
}; | ||
} | ||
|
||
export const casbinLinter = createLinter(ErrorType.MODEL); | ||
export const policyLinter = createLinter(ErrorType.POLICY); | ||
export const requestLinter = createLinter(ErrorType.REQUEST); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export enum ErrorType { | ||
MODEL = 'model', | ||
POLICY = 'policy', | ||
REQUEST = 'request', | ||
UNKNOWN = 'unknown', | ||
} | ||
|
||
interface ErrorDetector { | ||
type: ErrorType; | ||
detect: (error: string) => boolean; | ||
} | ||
|
||
// Request | ||
const REQUEST_ERROR_PATTERNS = [] as const; | ||
|
||
// Policy | ||
const POLICY_ERROR_PATTERNS = [] as const; | ||
|
||
// Model | ||
const MODEL_ERROR_PATTERNS = ['missing required sections'] as const; | ||
|
||
export const errorDetectors: ErrorDetector[] = [ | ||
{ | ||
type: ErrorType.REQUEST, | ||
detect: (error: string) => { | ||
return ( | ||
REQUEST_ERROR_PATTERNS.some((pattern) => { | ||
return error.toLowerCase().includes(pattern); | ||
}) || error.toLowerCase().includes('rvals:') | ||
); | ||
}, | ||
}, | ||
{ | ||
type: ErrorType.POLICY, | ||
detect: (error: string) => { | ||
return POLICY_ERROR_PATTERNS.some((pattern) => { | ||
return error.toLowerCase().includes(pattern); | ||
}); | ||
}, | ||
}, | ||
{ | ||
type: ErrorType.MODEL, | ||
detect: (error: string) => { | ||
return ( | ||
MODEL_ERROR_PATTERNS.some((pattern) => { | ||
return error.toLowerCase().includes(pattern); | ||
}) || | ||
(!error.toLowerCase().includes('request') && !error.toLowerCase().includes('policy') && !error.toLowerCase().includes('rvals:')) | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
export function getErrorType(error: string): ErrorType { | ||
for (const detector of errorDetectors) { | ||
if (detector.detect(error)) { | ||
return detector.type; | ||
} | ||
} | ||
return ErrorType.UNKNOWN; | ||
} | ||
|
||
export interface ErrorHandlers { | ||
onModelError?: (error: string) => void; | ||
onPolicyError?: (error: string) => void; | ||
onRequestError?: (error: string) => void; | ||
onUnknownError?: (error: string) => void; | ||
} | ||
|
||
export function handleError(error: string, handlers: ErrorHandlers): void { | ||
const errorType = getErrorType(error); | ||
|
||
switch (errorType) { | ||
case ErrorType.MODEL: | ||
handlers.onModelError?.(error); | ||
break; | ||
case ErrorType.POLICY: | ||
handlers.onPolicyError?.(error); | ||
break; | ||
case ErrorType.REQUEST: | ||
handlers.onRequestError?.(error); | ||
break; | ||
case ErrorType.UNKNOWN: | ||
handlers.onUnknownError?.(error); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
let currentError: string | null = null; | ||
import { ErrorType, getErrorType } from './errorHandler'; | ||
|
||
export const setError = (error: string | null) => { | ||
interface ErrorState { | ||
message: string; | ||
type: ErrorType; | ||
line?: number; | ||
} | ||
|
||
let currentError: ErrorState | null = null; | ||
|
||
export function setError(error: ErrorState | null) { | ||
currentError = error; | ||
}; | ||
} | ||
|
||
export function getError() { | ||
return currentError; | ||
} | ||
|
||
export const getError = () => {return currentError}; | ||
export function parseError(errorMessage: string): ErrorState { | ||
const lineMatch = errorMessage.match(/line (\d+)/); | ||
return { | ||
message: errorMessage, | ||
type: getErrorType(errorMessage), | ||
line: lineMatch ? parseInt(lineMatch[1], 10) : undefined, | ||
}; | ||
} |