Skip to content

Commit 615b3bc

Browse files
Merge pull request #189 from coddingtonbear/issue_186
[#186, #172] Show better error messages for search APIs. Fixes #186.
2 parents 6d7cb6a + bd651ee commit 615b3bc

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const ERROR_CODE_MESSAGES: Record<ErrorCode, string> = {
1313
"Authorization required. Find your API Key in the 'Local REST API' section of your Obsidian settings.",
1414
[ErrorCode.ContentTypeSpecificationRequired]:
1515
"Content-Type header required; this API accepts data in multiple content-types and you must indicate the content-type of your request body via the Content-Type header.",
16+
[ErrorCode.InvalidContentType]:
17+
"Unknown or invalid Content-Type specified in Content-Type header.",
1618
[ErrorCode.InvalidContentInsertionPositionValue]:
1719
"Invalid 'Content-Insertion-Position' header value.",
1820
[ErrorCode.InvalidContentForContentType]:
@@ -40,6 +42,9 @@ export const ERROR_CODE_MESSAGES: Record<ErrorCode, string> = {
4042
"The 'Operation' header you provided was invalid.",
4143
[ErrorCode.PatchFailed]:
4244
"The patch you provided could not be applied to the target content.",
45+
[ErrorCode.InvalidSearch]: "The search query you provided is not valid.",
46+
[ErrorCode.ErrorPreparingSimpleSearch]:
47+
"Error encountered while calling Obsidian `prepareSimpleSearch` API.",
4348
};
4449

4550
export enum ContentTypes {

src/requestHandler.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,24 @@ export default class RequestHandler {
991991
const results: SearchResponseItem[] = [];
992992

993993
const query: string = req.query.query as string;
994+
if (!(typeof query === "string")) {
995+
return this.returnCannedResponse(res, {
996+
message: "A single '?query=' parameter is required.",
997+
errorCode: ErrorCode.InvalidSearch,
998+
});
999+
}
9941000
const contextLength: number =
9951001
parseInt(req.query.contextLength as string, 10) ?? 100;
996-
const search = prepareSimpleSearch(query);
1002+
let search: ReturnType<typeof prepareSimpleSearch>;
1003+
try {
1004+
search = prepareSimpleSearch(query);
1005+
} catch (e) {
1006+
console.error("Could not prepare simple search: ", e);
1007+
return this.returnCannedResponse(res, {
1008+
message: `${e}`,
1009+
errorCode: ErrorCode.ErrorPreparingSimpleSearch,
1010+
});
1011+
}
9971012

9981013
for (const file of this.app.vault.getMarkdownFiles()) {
9991014
const cachedContents = await this.app.vault.cachedRead(file);
@@ -1099,11 +1114,16 @@ export default class RequestHandler {
10991114
};
11001115
const contentType = req.headers["content-type"];
11011116

1102-
if (!handlers[contentType]) {
1117+
if (!contentType) {
11031118
this.returnCannedResponse(res, {
11041119
errorCode: ErrorCode.ContentTypeSpecificationRequired,
11051120
});
11061121
return;
1122+
} else if (!handlers[contentType]) {
1123+
this.returnCannedResponse(res, {
1124+
errorCode: ErrorCode.InvalidContentType,
1125+
});
1126+
return;
11071127
}
11081128

11091129
try {

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IPeriodicNoteSettings } from "obsidian-daily-notes-interface";
55
export enum ErrorCode {
66
TextContentEncodingRequired = 40010,
77
ContentTypeSpecificationRequired = 40011,
8+
InvalidContentType = 40012,
89
InvalidContentForContentType = 40015,
910
InvalidContentInsertionPositionValue = 40050,
1011
MissingHeadingHeader = 40051,
@@ -17,10 +18,12 @@ export enum ErrorCode {
1718
PeriodIsNotEnabled = 40060,
1819
InvalidFilterQuery = 40070,
1920
PatchFailed = 40080,
21+
InvalidSearch = 40090,
2022
ApiKeyAuthorizationRequired = 40101,
2123
PeriodDoesNotExist = 40460,
2224
PeriodicNoteDoesNotExist = 40461,
2325
RequestMethodValidOnlyForFiles = 40510,
26+
ErrorPreparingSimpleSearch = 50010,
2427
}
2528

2629
export interface LocalRestApiSettings {

0 commit comments

Comments
 (0)