Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/app/controllers/postcodes_controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isEmpty, qToString } from "../lib/string";
import { Postcode } from "../models/postcode";
import { TerminatedPostcode } from "../models/terminated_postcode";
import { isValid } from "postcode";
import { chunk } from "../lib/chunk";
import { getConfig } from "../../config/config";
Expand Down Expand Up @@ -29,7 +30,11 @@ export const show: Handler = async (request, response, next) => {
if (!isValid(postcode.trim())) throw new InvalidPostcodeError();

const result = await Postcode.find(postcode);
if (!result) throw new PostcodeNotFoundError();
if (!result) {
// Check if postcode has been terminated
const terminated = await TerminatedPostcode.find(postcode);
throw new PostcodeNotFoundError(terminated);
}
response.jsonApiResponse = { status: 200, result: Postcode.toJson(result) };
next();
} catch (error) {
Expand Down
37 changes: 36 additions & 1 deletion api/app/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,44 @@ export class InvalidPostcodeError extends PostcodesioHttpError {
}
}

interface TerminatedPostcodeTuple {
id: number;
postcode: string;
pc_compact: string;
year_terminated: number;
month_terminated: number;
eastings: number;
northings: number;
longitude: number;
latitude: number;
location: string;
}

export class PostcodeNotFoundError extends PostcodesioHttpError {
constructor() {
public terminatedPostcode: TerminatedPostcodeTuple | null;

constructor(terminatedPostcode: TerminatedPostcodeTuple | null = null) {
super(404, "Postcode not found");
Object.setPrototypeOf(this, PostcodeNotFoundError.prototype);
this.terminatedPostcode = terminatedPostcode;
}

toJSON() {
const terminated = this.terminatedPostcode
? {
postcode: this.terminatedPostcode.postcode,
year_terminated: this.terminatedPostcode.year_terminated,
month_terminated: this.terminatedPostcode.month_terminated,
longitude: this.terminatedPostcode.longitude,
latitude: this.terminatedPostcode.latitude,
}
: null;

return {
status: this.status,
error: this.humanMessage,
...(terminated !== null && { terminated }),
};
}
}

Expand Down
4 changes: 2 additions & 2 deletions build/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.8.1">
<title data-rh="true">Page Not Found | Postcodes.io</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://postcodes.io/img/postcodesio.png"><meta data-rh="true" name="twitter:image" content="https://postcodes.io/img/postcodesio.png"><meta data-rh="true" property="og:url" content="https://postcodes.io/404.html"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="Page Not Found | Postcodes.io"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://postcodes.io/404.html"><link data-rh="true" rel="alternate" href="https://postcodes.io/404.html" hreflang="en"><link data-rh="true" rel="alternate" href="https://postcodes.io/404.html" hreflang="x-default"><link rel="stylesheet" href="/assets/css/styles.0cd2942a.css">
<script src="/assets/js/runtime~main.9ab8726d.js" defer="defer"></script>
<script src="/assets/js/main.ee9532f4.js" defer="defer"></script>
<script src="/assets/js/runtime~main.c5239b27.js" defer="defer"></script>
<script src="/assets/js/main.ed04f129.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><defs>
Expand Down
1 change: 0 additions & 1 deletion build/assets/js/1807015c.5bbae310.js

This file was deleted.

1 change: 1 addition & 0 deletions build/assets/js/1807015c.6f773d8e.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion build/assets/js/4c5e977b.a5e1a0bf.js

This file was deleted.

1 change: 1 addition & 0 deletions build/assets/js/4c5e977b.df474580.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build/assets/js/5453.30db375d.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions build/assets/js/5453.5501928c.js

This file was deleted.

2 changes: 2 additions & 0 deletions build/assets/js/main.ed04f129.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions build/assets/js/main.ee9532f4.js

This file was deleted.

Loading