-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more tests for various kinds of errors, regroup errors in same module
This mostly improves testing and code organization, it does not fix anything
- Loading branch information
Showing
10 changed files
with
267 additions
and
112 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,9 +1,96 @@ | ||
import type { XmlDocument, XmlElement } from '@rgrove/parse-xml'; | ||
import { | ||
findChildElement, | ||
getElementAttribute, | ||
getElementName, | ||
getElementText, | ||
getRootElement, | ||
stripNamespace, | ||
} from '../shared/xml-utils.js'; | ||
|
||
export class EndpointError extends Error { | ||
constructor( | ||
public message: string, | ||
public httpStatus?: number, | ||
public isCrossOriginRelated?: boolean | ||
message: string, | ||
public readonly httpStatus?: number, | ||
public readonly isCrossOriginRelated?: boolean | ||
) { | ||
super(message); | ||
} | ||
} | ||
|
||
/** | ||
* Representation of an Exception reported by an OWS service | ||
* | ||
* This is usually contained in a ServiceExceptionReport or ExceptionReport | ||
* document and represented as a ServiceException or Exception element | ||
*/ | ||
export class ServiceExceptionError extends Error { | ||
/** | ||
* Constructor | ||
* @param message Error message | ||
* @param requestUrl URL which resulted in the ServiceException | ||
* @param code Optional ServiceException code | ||
* @param locator Optional ServiceException locator | ||
* @param response Optional response content received | ||
*/ | ||
public constructor( | ||
message: string, | ||
public readonly requestUrl?: string, | ||
public readonly code?: string, | ||
public readonly locator?: string, | ||
public readonly response?: XmlDocument | ||
) { | ||
super(message); | ||
} | ||
} | ||
|
||
/** | ||
* Parse a ServiceException element to a ServiceExceptionError | ||
* @param serviceException ServiceException element | ||
* @param url URL from which the ServiceException was generated | ||
*/ | ||
export function parse( | ||
serviceException: XmlElement, | ||
url?: string | ||
): ServiceExceptionError { | ||
const errorCode = | ||
getElementAttribute(serviceException, 'code') || | ||
getElementAttribute(serviceException, 'exceptionCode'); | ||
const errorLocator = getElementAttribute(serviceException, 'locator'); | ||
const textElement = | ||
findChildElement(serviceException, 'ExceptionText') || serviceException; | ||
const errorMessage = getElementText(textElement).trim(); | ||
return new ServiceExceptionError( | ||
errorMessage, | ||
url, | ||
errorCode, | ||
errorLocator, | ||
serviceException.document | ||
); | ||
} | ||
|
||
/** | ||
* Check the response for a ServiceExceptionReport and if present throw one | ||
* @param response Response to check | ||
* @param url URL from which response was generated | ||
*/ | ||
export function check(response: XmlDocument, url?: string): XmlDocument { | ||
const rootEl = getRootElement(response); | ||
const rootElName = stripNamespace(getElementName(rootEl)); | ||
if (rootElName === 'ServiceExceptionReport') { | ||
// document contains a ServiceExceptionReport, so generate an Error from | ||
// the first ServiceException contained in it | ||
const error = findChildElement(rootEl, 'ServiceException'); | ||
if (error) { | ||
throw parse(error, url); | ||
} | ||
} | ||
if (rootElName === 'ExceptionReport') { | ||
const error = findChildElement(rootEl, 'Exception'); | ||
if (error) { | ||
throw parse(error, url); | ||
} | ||
} | ||
// there was nothing to convert to an Error so just pass the document on | ||
return response; | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.